jQguru
jQguru

Reputation: 267

Java static variable updates

Below you can see a static variable counter in a Java class.

The question is when will this variable reset? For example, when I restart the program, computer. What are the other possible scenarios it can reset?

Another question is: what could be the reasons for this variable to increase by less than the number of times the function do() is executed? For example, could it be something with starting multiple processes of the class java Whatever? Or could it be something with multiple threads/servers, etc?

class Whatever {

    static int counter = 0;

    function do() {
        counter++;
        //...
    }

}

Additional question: If multiple threads execute function do(), how will the counter variable behave? It will be less than the number of times function do() was executed?

Upvotes: 5

Views: 13683

Answers (7)

João Simões
João Simões

Reputation: 1361

A static variable means that there are only one incarnation of that field during a program execution. It is loaded when the class is initialized.

For your second question, your variable isn't thread safe because multiple threads can access it at the same time. Even if your counter was volatile you would still face concurrency problem. You have three options, given your example and requirements:

If your only requirement is to manipulate the variable and the rest of the code won't depend of that you can use synchronize (killing a fly with a cannon) or AtomicInteger (the choice with better performance):

static synchronize int counter = 0;
// or
static AtomicInteger counter = new AtomicInteger();

If the rest of your code is dependent of your counter, you must use a lock object or synchronize your method:

class Whatever {

    static int counter = 0;

    synchronize function do() {
        counter++;
        if(counter < 10){
            //  do something
        }
    }
}

//  or

class Whatever {

    static int counter = 0;
    static final Object _lock = new Object();

    function do() {
        synchronized (_lock) {
            counter++;
            if(counter < 10){
                //  do something
            }
        }
    }
}

This are the options that are in my head right now, but probably there are more.

Upvotes: 0

jlordo
jlordo

Reputation: 37813

class Foo { // in same package
public static void main(String[] args) {
    Whatever w = new Whatever();
    for (int i = 0; i < 1000; i++) {
        w.do();
    }
    Whatever.counter = -1;
}

here do() is invoked 1000 times, but counter will have value at the end.

I used do() like you in your example, but note that is not a valid method name, because it's the keyword for a do while loop.

Upvotes: 0

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

The question is when will this variable reset?

static variable can be reset using custom reset() method. If You say restart program, theoretically that variable will be initialized to it's value not reinitialized as it is not same(you restart program).

Upvotes: 0

PC.
PC.

Reputation: 7024

counter is not a private variable. So it is possible that this value is changed by some other class.

This variable will get reset whenever your program (or specifically the container/jvm) is restated.

Upvotes: 1

SJuan76
SJuan76

Reputation: 24885

1) The variable is set(reset) when the class is loaded. Apart from shutting down the JVM, some servers load the class in different applications (v.g., the Tomcat webapps) and some of them allow restarting them.

2) Concurrent modification by threads. But it should be rare, unless you use it a lot. Use synchronized for the function/block.

Upvotes: 0

Jatin
Jatin

Reputation: 31724

According to the JLS:

If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized

So this answers your first question. i.e.e exactly when the class is loaded :)

As per second question, nope. if the variable is declared private. Then the only access is via the method because of encapsulation.

Static variables lasts till the JVM is shutdown.

Upvotes: 2

Philipp
Philipp

Reputation: 69663

A static variable will be re-initialized when you restart the application.

Upvotes: 2

Related Questions