In this example
@Override
public void run() {
new StaticCounter().setVisible(true);
}
There's not much to see since you only have one StaticCounter
object. When you click the Counter++
button, the value goes up. Good 'till here?
In this one
@Override
public void run() {
new StaticCounter().setVisible(true);
new StaticCounter().setVisible(true);
}
It's more easy to see static
behavior. When you click one of the JFrame
's Counter++
button, you see the value go up. Obviously it won't change the other window because you haven't redrawn it, but if you click the other's button, you will see that the next value continues from where the static
counter was. This proves that there is only one instance of the counter in the JVM.