0

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?

awksp
  • 11,764
  • 4
  • 37
  • 44
  • What would you say the definition of "static" is in this kind of situation? – awksp Jul 16 '15 at 21:27
  • If all the state in the class is static, you don't need to instantiate it. You can access its variables via `Stats.numOne` etc. – khelwood Jul 16 '15 at 21:30

6 Answers6

1

No, the variables will maintain state without the static modifier

andrewdleach
  • 2,458
  • 2
  • 17
  • 25
0

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?

Community
  • 1
  • 1
Adam Francey
  • 133
  • 6
0

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;
xenteros
  • 15,586
  • 12
  • 56
  • 91
0

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 (e.g. a singleton instance).

akezzou
  • 11
  • 2
0

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 i.e 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

Rahul Prasad
  • 747
  • 1
  • 9
  • 13
0

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