What is a final variable in java?

Java 8Object Oriented ProgrammingProgramming

A The final keyword can be used with class method and variable. A final class cannot be instantiated, a final method cannot be overridden and a final variable cannot be reassigned.

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to a different object. However, the data within the object can be changed. So, the state of the object can be changed but not the reference. With variables, the final modifier often is used with static to make the constant a class variable.

Example

public class Test {
   final int value = 10;
   public static final int BOXWIDTH = 6;
   static final String TITLE = "Manager";
   
   public void changeValue() {
      value = 12;
   }
}
raja
Ali
Published on 12-Feb-2018 15:09:28
Advertisements