Final static variable in Java

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:

  1. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate the behavior of
    // final static variable
    class Test {
        final static int x;
        public static void main(String[] args)
        {
        }
    }

    chevron_right

    
    

    Output:

    error: variable x not initialized in the default constructor
    
  2. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    10
    
  3. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate
// that we can't declare
// final static variable
// within any non-static blocks or methods
class Test {
    final static int x;
    public static void m()
    {
        x = 10;
    }
    public static void main(String[] args)
    {
        System.out.println(x);
    }
}

chevron_right


Output:

error: cannot assign a value to final variable x

Implementation of final static variable

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
    }
}

chevron_right


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.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.