简体   繁体   中英

Static final member variables vs. get methods

I have a class with all the configuration from a property file.

My first solution is this:

public class Config {

    public static final int disc;

    static {

        // Read property file and set properties

        disc = 5;
    }
}

Reading the information in this way:

System.out.println(Config.disc);

The second solution is:

public class Config {

    private int disc;

    public void Config() {

        // Read property file and set properties

        disc = 5;
    }

    public int getDisc() {
        return this.disc;
    }
}

Reading in that way:

System.out.println(new Config().getDisc());

What's the best way and why? What the advantages and disadvantages?

The answer depends on the meaning of disc :

  • If disc represents a constant, essentially giving a name to a numeric value, than a public final field is better
  • If disc represents a value that could change as the result of user's actions (ie if it is part of configuration) then a private variable with a getter is preferable.

The second approach gives you more flexibility, should you decide to refactor your class in the future: it lets you initialize the private disc at a later time, or replace it with some other way of obtaining a value, eg by computing it from other values, or reading from another object.

using getDisc() you can have one disc variable per object while static disc is shared across all the instances.

  • If you have different disc value for every instance of class, then go for second approach.
  • If you have same value which needs to be shared across all the instances of the class, then use the first approach

By they way, you cannot do System.out.println(Config.getDisc()); . You can't call a non-static method using class name

What about: public static final int DISC = 5; Then you can use Config.DISC to access your config value.

System.out.println(Config.getDisc());

This is wrong. You can't call a non-static method with class name. You must create an object as following:

Config cfg = new Config();
System.out.println(cfg.getDisc());

Now, In your first case all the instances will share same copy of disc . so, if it's not a constant, go for 2nd case.

Software Engineering Principles requires " Information Hiding ". In your first solution your "disc" property is public and this property can be used in anywhere and it breaks "information hiding" principle. I vote for your second solution and recommend you to make "disc" private.

What you want in this case is really

public static final int DISC = 5;

System.out.println(Config.DISC);

(Note that members like these are conventionally always written in capticals).


Your first solution has an error in it. It won't compile because you try to set a final field multiple times. Each time you create a new Config() , you will assign a value to disc , but since it is final that won't work. What you probably wanted to suggest was to put it in the static constructor:

public class Config
{
    public static final int DISC;
    static
    {
        DISC = 5;
    }
}

However, I do not recommend it, because is is totaly find to do it the first way I wrote and is much easier as well.

Your second solution has an error in it. It won't compile because you can't access the non-static method from a static context. This means that you first will have to create an instance of Config, before you can call the getDisc() method. The better solution in this case is to declare the method as static.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM