2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Seems to me like you worry to much. Have you heard the saying about premature optimization? \$\endgroup\$ Commented Jan 31, 2016 at 0:25
  • \$\begingroup\$ I haven't, I'll look it up. But yes, that's quite possible, even with websites I worry a lot about optimizing my code the best way possible, even if it probably only makes 1 cpu-cycle of difference haha \$\endgroup\$ Commented Jan 31, 2016 at 0:27

1 Answer 1

4
\$\begingroup\$

There won't be any GC overhead since there won't be any instantiated instances to collect.

I think there's a small overhead by accessing a class' static field rather than an in-scope variable ; however, I would consider it largely worth the improved readability/maintainability.

I guess you could also argue you'll use up a little bit more of your permGen / Meta space, but I've never heard it be a consequent problem.

\$\endgroup\$
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.