Skip to main content
added 2 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

java - using Using a final class for string constants

I want to use something like Setting.RES_WIDTH in my code without the need for a getter function (iei.e.: custom getter, enum's .name(), .toString() or .valueOf()).

Someone suggested using a final class, so I wrote this.:

public final class Setting {

    public final static String RES_WIDTH        = "resolution_width";
    public final static String RES_HEIGHT       = "resolution_height";
    public final static String FULLSCREEN       = "fullscreen_enabled";
    public final static String VSYNC            = "vsync_enabled";
    
    //Prevent someone from creating an instance of this class
    private Setting() {}
}

This code works, but I am wondering if there are any kind of issues (performance, GC, etc) that might arise from using this.

java - using a final class for string constants

I want to use something like Setting.RES_WIDTH in my code without the need for a getter function (ie: custom getter, enum's .name(), .toString() or .valueOf()).

Someone suggested using a final class, so I wrote this.

public final class Setting {

    public final static String RES_WIDTH        = "resolution_width";
    public final static String RES_HEIGHT       = "resolution_height";
    public final static String FULLSCREEN       = "fullscreen_enabled";
    public final static String VSYNC            = "vsync_enabled";
    
    //Prevent someone from creating an instance of this class
    private Setting() {}
}

This code works, but I am wondering if there are any kind of issues (performance, GC, etc) that might arise from using this.

Using a final class for string constants

I want to use something like Setting.RES_WIDTH in my code without the need for a getter function (i.e.: custom getter, enum's .name(), .toString() or .valueOf()).

Someone suggested using a final class, so I wrote this:

public final class Setting {

    public final static String RES_WIDTH        = "resolution_width";
    public final static String RES_HEIGHT       = "resolution_height";
    public final static String FULLSCREEN       = "fullscreen_enabled";
    public final static String VSYNC            = "vsync_enabled";
    
    //Prevent someone from creating an instance of this class
    private Setting() {}
}

This code works, but I am wondering if there are any kind of issues (performance, GC, etc) that might arise from using this.

Source Link

java - using a final class for string constants

I want to use something like Setting.RES_WIDTH in my code without the need for a getter function (ie: custom getter, enum's .name(), .toString() or .valueOf()).

Someone suggested using a final class, so I wrote this.

public final class Setting {

    public final static String RES_WIDTH        = "resolution_width";
    public final static String RES_HEIGHT       = "resolution_height";
    public final static String FULLSCREEN       = "fullscreen_enabled";
    public final static String VSYNC            = "vsync_enabled";
    
    //Prevent someone from creating an instance of this class
    private Setting() {}
}

This code works, but I am wondering if there are any kind of issues (performance, GC, etc) that might arise from using this.