Final static variable in Java - GeeksforGeeks

22 Sep, 2017
Prerequisite : static variables, final keyword
Static variable: When the value of a variable is not varied, then it is a not good choice to go for instance variable. At that time we can add static modifier to that variable. Whenever we declare variable as static, then at the class level a single variable is created which is shared with the objects. Any change in that static variable reflect to the other objects operations. If we won’t initialize a static variable, then by default JVM will provide a default value for static variable.
But when we declare a static variable with final modifier then we should take care of the following conventions:
Declaring variables only as static can lead to change in their values by one or more instances of a class in which it is declared.
Declaring them as static final will help you to create a CONSTANT. Only one copy of variable exists which can’t be reinitialize.
Important points about final static variable:
Initialization of variable Mandatory : If the static variable declared as final, then we have to perform initialization explicitly whether we are using it or not and JVM won’t provide any default value for the final static variable.// Java program to illustrate the behavior of// final static variableclass Test {    final static int x;    public static void main(String[] args)    {    }}Output:error: variable x not initialized in the default constructor
Initialization before class loading : For final static variable, it is compulsory that we should perform initialization before class loading completion. We can initialize a final static variable at the time of declaration.// Java program to illustrate that final// static variable can be initialized// at the time of declarationclass Test {    final static int x = 10;    public static void main(String[] args)    {        System.out.println(x);    }}Output:10
Initialize inside a static block : We can also initialize a final static variable inside a static block because we should initialize a final static variable before class and we know that static block is executed before main() method.// Java program to illustrate that final// static variable can be initialized// inside static blockclass Test {    final static int x;    static    {        x = 10;    }    public static void main(String[] args)    {        System.out.println(x);    }}Output:10

Initialization of variable Mandatory : If the static variable declared as final, then we have to perform initialization explicitly whether we are using it or not and JVM won’t provide any default value for the final static variable.// Java program to illustrate the behavior of// final static variableclass Test {    final static int x;    public static void main(String[] args)    {    }}Output:error: variable x not initialized in the default constructor

// Java program to illustrate the behavior of// final static variableclass Test {    final static int x;    public static void main(String[] args)    {    }}
Output:
error: variable x not initialized in the default constructor

Initialization before class loading : For final static variable, it is compulsory that we should perform initialization before class loading completion. We can initialize a final static variable at the time of declaration.// Java program to illustrate that final// static variable can be initialized// at the time of declarationclass Test {    final static int x = 10;    public static void main(String[] args)    {        System.out.println(x);    }}Output:10

// Java program to illustrate that final// static variable can be initialized// at the time of declarationclass Test {    final static int x = 10;    public static void main(String[] args)    {        System.out.println(x);    }}
Output:
10

Initialize inside a static block : We can also initialize a final static variable inside a static block because we should initialize a final static variable before class and we know that static block is executed before main() method.// Java program to illustrate that final// static variable can be initialized// inside static blockclass Test {    final static int x;    static    {        x = 10;    }    public static void main(String[] args)    {        System.out.println(x);    }}Output:10

// Java program to illustrate that final// static variable can be initialized// inside static blockclass Test {    final static int x;    static    {        x = 10;    }    public static void main(String[] args)    {        System.out.println(x);    }}
Output:
10

Apart from the above mentioned methods, if we try to initialize a final static variable anywhere else then we will get compile time error.
// Java program to illustrate// that we can't declare// final static variable// within any non-static blocks or methodsclass Test {    final static int x;    public static void m()    {        x = 10;    }    public static void main(String[] args)    {        System.out.println(x);    }}
Output:
error: cannot assign a value to final variable x

Implementation of final static variable
class MainClass {    final static String company = "GFG";    String name;    int rollno;public    static void main(String[] args)    {        MainClass ob = new MainClass();          // If we create a database for GFG org        // then the company name should be constant        // It can’t be changed by programmer.        ob.company = "Geeksforgeeks";          ob.name = "Bishal";        ob.rollno = 007;        System.out.println(ob.company);        System.out.println(ob.name);        System.out.println(ob.rollno);    }}
Output:
error: cannot assign a value to final variable company

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-final keyword
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Initialize an ArrayList in Java
HashMap in Java with Examples
Object Oriented Programming (OOPs) Concept in Java
ArrayList in Java
How to iterate any Map in Java
Multidimensional Arrays in Java
LinkedList in Java
Stack Class in Java
Overriding in Java
Set in Java