简体   繁体   中英

When making a class to hold variables should the variables always be static?

Say I wanted to make a class to hold a set of integers that would be accessed from multiple other classes and instances. I don't want them reverting to the value they had when the code was compiled. Does that mean they have to be static, in order to keep them from going back their original value? For example

The original stats holding class here:

public class Stats() {

    public static int numOne = 0;
    public static int numTwo = 5;
    public static int numThree = 3
    //etc...

}

It is called on in two places. Here:

public class exampleClass() {

    private Stats stats = new Stats();
    stats.numOne += 5;
    //More variable changes.

}

Also here:

public class exampleClassTwo() {

    private Stats stats = new Stats();
    stats.numOne -= 3;
    //More variable changes.

}

Will these calls reset the variables to their original class value if the variables are not static? If so, does that mean they should always be static?

不,没有静态修饰符的变量将保持状态

Yes, when you instantiate an object, variables will be initialized to the class values when they are not static.

When a variable has the static keyword, that variable value persists over all instances: the two places you called it each create an object, both objects have the same values for their static variables (even if they are changed).

Variables without the static keyword are unique to the instance: changing it on one object doesn't affect its value on the other.

See here for more info: What does the 'static' keyword do in a class?

No. You would use static key word for using those values without initializating them.

public class Stats() {

    public static int numOne = 0;
    public static int numTwo = 5;
    public static int numThree = 3
    //etc...
}

public class exampleClass() {

    int a = 0;
    a += Stats.numThree;
    System.out.println(a);
}

>>> 3;

No need for static attributes in your case indeed, each class instance will contain a private copy of attributes initialized at instance creation time, and records all subsequent modifications until object is deleted (in java it means no longer referenced).

Main usage for static is either to store constants or global state (eg a singleton instance).

Doing,

private Stats stats = new Stats();
    stats.numOne += 5; 

Kind of defeats the purpose of having numOne as static. The static field numOne should be accessed in a static way ie as follows: Stats.numOne

static variables are Class variables and are used when we want to maintain a value across instances of the class. So modifying the value of numOne across various functions will keep on changing the value of class variable numOne. Run the following code to see the effect of having a class variable in a class:

public class StaticVarDemo {
    public static int staticCount =0 ;
    public StaticVarDemo(){
        staticCount++;
    }
    public static void main(String[] args) {
        new StaticVarDemo();
        StaticVarDemo.staticCount +=5;
        System.out.println("staticCount : " + StaticVarDemo.staticCount);
        new StaticVarDemo();
        new StaticVarDemo();
        System.out.println("staticCount : "+staticCount);
    }
} 

It will give the output: staticCount : 6 staticCount : 8

It seems after some research a singleton did the job. Creating one singular instance but calling on it more then once.

See Here: http://www.tutorialspoint.com/java/java_using_singleton.htm

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