final Variable in Java

final Instance Variable

If the value of a variable is varied from object to object such type of variables are called instance variables.

  • For every object, a separate copy of the instance variable will be created.
  • For instance, for variables we are not required to perform initialization explicitly, the default value will always provide by JVM.
public class TheCodeData {
    int x;
    public static void main(String[] args) {
        TheCodeData obj = new TheCodeData();
        System.out.println(obj.x);
   }
   }
final variable in Java

If the instance variable is declared as final then it is compulsory that we have to perform initialization explicitly, whether we are using it or not in the case of the final instance variable JVM will not provide a default value and if we don’t initialize the final instance variable then we will get a compilation error – “Variable ‘x’ might not have been initialized”.

public class TheCodeData {
    final int x;
   }
instance variable Java

Rule for final Instance Variables

For the final instance variable it is compulsory that we have to perform initialization before constructor compilation following are the various options for initialization.

  • At the time of declaration
class TheCodeData{
  final int var = 1;
}
  • Inside Instance Block
public class TheCodeData {
    final int x;{
        x = 1;
    }
}
  • Inside Constructor
public class TheCodeData {
    final int x;

    public TheCodeData() {
        x=10;
    }
}

These are the only possible ways to initialize the final instance variable, if we tried to initialize the final instance variable anywhere else then we will get compile time error- “Cannot assign a value to final variable ‘x'”.

public class TheCodeData {
    final int x;

    public static void main(String[] args) {
        x = 10;
    }
}
final variable in Java

final static Variable

If the value of a variable is not varied from object to object such type of variable is not recommended to declare as an instance variable, we have to declare those types of variables at the class level by using a static modifier.

In the case of an instance variable for every object, a separate copy will be created but in the case of a static variable, a single copy will be created at the class level and shared by every object of that class.

  • For static variables it is not required to perform initialization explicitly JVM will always provide default values.
public class TheCodeData {
   static int x;

    public static void main(String[] args) {
        System.out.println(x);
    }
    }
final static varaible

  • If the static variable is declared as final then it is compulsory to perform initialization explicitly otherwise we will get a compilation error- “variable x not initialized in the default constructor” and JVM will not provide any default value.
public class TheCodeData {
   final static int x;

    public static void main(String[] args) {
        System.out.println(x);
    }
    }
final static varaible

Rules for final static Variables

For the final static variable we have to perform initialization explicitly following are the various options to perform initialization.

  • At the time of declration
public class TheCodeData {
   final static int x = 1;
    }
  • Inside static block
public class TheCodeData {
   final static int x;
   static {
       x = 1;
   }
    }

These are the only possible options to perform initialization for the final static variable if we try to perform initialization anywhere else then we will get compilation error_”Cannot assign a value to final variable ‘x'”.

public class TheCodeData {
final static int x;
public void method() {
x = 1;
}
}
final static variable

final Local Variables

Sometimes for temporary requirements, we have to declare variables inside a method or block, or constructor such types of variables are called local variables or temporary variables.

For local variables, JVM would not provide any default value compulsory. we should perform initialization explicitly before using the local variable. if we did not perform initialization for the local variable and we are using that local variable then we will get a compilation error-“variable x might not have been initialized”.

public class TheCodeData {
    public static void main(String[] args) {
        int x;
        System.out.println(x);
   }
   }

local variable

But if we are not using local variable then it’s not compulsory to perform initialization for local variables

public class TheCodeData {
    public static void main(String[] args) {
        int x;
        System.out.println("The Code That");
   }
   }

local variable in Java

Even though the local variable is final, then also we don’t have to perform initialization i.e. if we are not using the local variable then we don’t have to perform initialization even though it’s final

public class TheCodeData {
    public static void main(String[] args) {
       final int x;
        System.out.println("The Code Data");
    }
}
final local variable

Final is the only applicable modifier for a local variable, if by mistake we will try to use any other modifier then we will get compile time error – “java: illegal start of expression”

public class TheCodeData {
    public static void main(String[] args) {
       private int x;
        System.out.println("yh");
    }
}
java: illegal start of expression

Note – if we are not declaring any modifier then by default it’s the default, but this rule is only applicable for static and instance variables not for local variable

Formal Parameter

Formal parameters are those variables that are written in the definition of the function with its correct data type. A formal parameter of a method simply acts as a local variable, hence formal parameters can be declared as final.

If formal parameters are declared as final, then within the method we can’t perform re-assignment.

public class TheCodeData {
    public static void main(String[] args) {
      add(1,2);
    }
    public static void add(final int a, final int b){
        a=20;
        b=10;
        System.out.println(a+b);
    }
}
final formal parameter

Similar Java Tutorials

6 thoughts on “final Variable in Java”

Leave a Comment