Final Instance VariablesS2C Home « Final Instance Variables

In the last lesson we looked at Instance Variables & Scope, in this lesson we finish our examination of instance variables by examining instance variable constants.

We make the value of an instance variable a constant by using the final access modifier which means that once assigned a value it can never be changed.

Final Instance Variable Rules Top

1. You must always assign a value to an instance variable marked as final before using it or you get a compiler error:


package com.server2client;
/*
  Using a final instance variable before assignment
*/ 
public class FinalInstanceVarUnassigned {
    final int i;

    FinalInstanceVarUnassigned() {
        System.out.println(i);
    }   
}

run final instance variable unassigned
Screenshot 1. Compiling the FinalInstanceVarUnassigned class.

The above screenshot shows the result of trying to compile class FinalInstanceVarUnassigned.

2. An instance variable that is marked as final can never change once it has been assigned a value.


package com.server2client;
/*
  Trying to reassign a final instance variable
*/ 
public class FinalInstanceChanged {
    final int i = 1;

    FinalInstanceChanged() {  // Constructor
        i = 2;
    }   
}

run final instance variable changed
Screenshot 2. Compiling the FinalInstanceChanged class.

The above screenshot shows the result of trying to compile class FinalInstanceChanged.

3. An instance variable that is marked as final can only be assigned values when declared or in its constructor or we get a compiler error:


package com.server2client;
/*
  Assigning final instance variable outside declaration and construction
*/ 
public class FinalInstanceAfterConst {
    final int i;
    
    FinalInstanceAfterConst() {
        assignFinal();
        System.out.println(i);
    }

    public void assignFinal() {
        i = 1;
    }
}

run final instance variable after constructor
Screenshot 3. Compiling the FinalInstanceAfterConst class.

The above screenshot shows the result of trying to compile class FinalInstanceAfterConst.

Final Instance Variable Correct Use Top

The following three examples show correct initialisation of final variables at declaration or via the constructor when no inital value is assigned to a final variable:

At Declaration Top

Assign final value variable at declaration.


package com.server2client;
/*
  Assigning final instance variable at declaration
*/ 
public class FinalInstanceAtDecl {
    final int i = 678;
    
    FinalInstanceAtDecl() {
        System.out.println(this.i);
    }

    public static void main (String[] args) {
        FinalInstanceAtDecl fiad = new FinalInstanceAtDecl();
    }
}

run final instance variable at declaration
Screenshot 4. Running the FinalInstanceAtDecl class.

The above screenshot shows the result of running the FinalInstanceAtDecl class.

Via Constructor Parameter Top

Assign final value via constructor parameter.


package com.server2client;
/*
  Assigning final instance variable via constructor parameter
*/ 
public class FinalInstanceByConstParam {
    final int i;
    
    FinalInstanceByConstParam(int i) {
        this.i = i;
        System.out.println(this.i);
    }

    public static void main (String[] args) {
        FinalInstanceByConstParam fibcp = new FinalInstanceByConstParam(2706);
    }
}

run final instance variable via constructor parameter
Screenshot 5. Running the FinalInstanceByConstParam class.

The above screenshot shows the result of running the FinalInstanceByConstParam class. There is a lengthy discussion on using the this keyword in the Overloaded Constructors and this lesson, for now you just need to know that it passes an implicit reference to the invoking object.

Within Constructor Top

Assign final value within constructor.


package com.server2client;
/*
  Assigning final instance variable within constructor
*/ 
public class FinalInstanceWithinConst {
    final int i;
    
    FinalInstanceWithinConst() {
        i = 8657;
        System.out.println(i);
    }

    public static void main (String[] args) {
        FinalInstanceWithinConst fiwc = new FinalInstanceWithinConst();
    }
}

run final instance variable within constructor
Screenshot 6. Running the FinalInstanceWithinConst class.

The above screenshot shows the result of running the FinalInstanceWithinConst class.

Related Quiz

Objects & Classes Quiz 9 - Final Instance Variables

Lesson 9 Complete

This lesson was about final instance variables and how to use them correctly.

What's Next?

In the next lesson we learn about constructors, which we can use to instantiate objects of our classes.