java - Static final member variables vs. get methods -
i have class configuration property file.
my first solution this:
public class config { public static final int disc; static { // read property file , set properties disc = 5; } } reading information in way:
system.out.println(config.disc); the second solution is:
public class config { private int disc; public void config() { // read property file , set properties disc = 5; } public int getdisc() { return this.disc; } } reading in way:
system.out.println(new config().getdisc()); what's best way , why? advantages , disadvantages?
the answer depends on meaning of disc:
- if
discrepresents constant, giving name numeric value, public final field better - if
discrepresents value change result of user's actions (i.e. if part of configuration) private variable getter preferable.
the second approach gives more flexibility, should decide refactor class in future: lets initialize private disc @ later time, or replace other way of obtaining value, e.g. computing other values, or reading object.
Comments
Post a Comment