Instance variable as final in Java

Instance variable: As we all know that when the value of variable is varied from object to object then that type of variable is known as instance variable. The instance variable is declared inside a class but not within any method, constructor, block etc. If we don’t initialize an instance variable, then JVM automatically provide default value according to the data type of that instance variable.
But if we declare an instance variable as final, then we must have to take care about the behavior of instance variable.

  • There is no requirement as such to make the variable final. However, when it is true that you explicitly intend to never change the variable, it is often good practice to make it final
  • We have to use instance variable as final in the creation of immutable class.

Important points about final instance variable:

  1. Initialization of variable Mandatory : If the instance 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 instance variable.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate the behavior 
    // of final instance variable
    class Test {
        final int x;
        public static void main(String[] args)
        {
            Test t = new Test();
            System.out.println(t.x);
        }
    }

    chevron_right

    
    

    Output:

    error: variable x not initialized in the default constructor
    
  2. Initialization before constructor completion : For final instance variable we have to perform initialization before constructor completion. We can initialize a final instance variable at the time of declaration.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    10
    
  3. Initialize inside a non-static or instance block : We can also initialize a final instance variable inside a non-static or instance block also.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Output:

    10
    
  4. Initialization in default constructor : Inside default constructor we can also initialize a final instance variable.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to illustrate that 
    // final instance variable
    // can be initialized 
    // in the default constructor
    class Test {
        final int x;
        Test()
        {
            x = 10;
        }
        public static void main(String[] args)
        {
            Test t = new Test();
            System.out.println(t.x);
        }
    }

    chevron_right

    
    

    Output:

    10
    

The above mentioned are the only possible places to perform initialization for final instance variable. If we try to perform initialization 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 cant declare
// final instance variable 
// within any static blocks
class Test {
    final int x;
    public static void main(String[] args)
    {
        x = 10;
        Test t = new Test();
        System.out.println(t.x);
    }
}

chevron_right


Output:

error: non-static variable x cannot be referenced from a static context
filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate that we
//  cant declare or initialize
// final instance variable within any methods
class Test {
    final int x;
    public void m()
    {
        x = 10;
    }
}

chevron_right


Output:

error: cannot assign a value to final variable x

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 :


Be the First to upvote.


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