Static variables vs final Static variables

The match has finally arrive ladies and gentlemen .

The most anticlimactic fight in java, nevertheless it’s still important to understand the difference between them .

When we declare a variable to be static . We create a single copy is made and shared between all objects at the class level . So , static are basically global variables . Since all the instances of the class will share the same static variable .

Just remember a key point : Static variables are only created at class-level only .

(all code will be available down below this page with a link to the programs inside my GitHub )

Our output is 1 .

Now let’s look at final static variables

Final static will help our variables to be created as constants , as only one copy of the variable will/and exists and which can’t be reinitialize .

So , we created a final static variable int with a value of 10 .

Output

To see what I meant let’s try to initialize the same final static int somewhere else , and we’ll see an error :

https://github.com/ValorWind1/Java_Review_1.0/tree/master/src/java_tutorial/static_vs_finalStatic

https://github.com/ValorWind1/VS/blob/master/training/staticVariableDemo.java

Leave a comment