Java final keyword

The final is a very important keyword in java, which is used to restrict user. A programmer must not declare a variable or class with the name "Final" in a Java program. We can have final methods, final classes, final data members, final local variables and final parameters.

Java final keyword

The final is a very important keyword in java, which is used to restrict user. A programmer must not declare a variable or class with the name "Final" in a Java program. We can have final methods, final classes, final data members, final local variables and final parameters.

Java final keyword

Java final keyword

The final is a very important keyword in java, which is used to restrict user. A programmer must not declare a variable or class with the name "Final" in a Java program. We can have final methods, final classes, final data members, final local variables and final parameters.

When an array is declared as final, the state of the object stored in the array can be modified. It must be made immutable so that modifcations are not made.

The final keyword can be applied to:

  • method
  • class
  • variable

Syntax of final class:-

final class test 
 {
 //body of class // final class
 }

syntax of final variable:-

 class test1 
 {
 final int=20;//final variable
 }

Syntax of final method

 class test3 
 {
  final void run(){// final method
 }

Note:

  • The final keyword applied to a class, indicating the class cannot be extended (Subclassed).
  • The final keyword applied to method indicating the method cannot be overridden in any subclass.
  • The final variable can't be changed when final keyword use variable constant

Exampleof final jeyword in Java:

public class FinalExample {
	 
    public static void main(String[] args) {
 
        int variable=10;
 
        System.out.println("Final Variable:"+variable);
 
        variable=15; 

    }
 
    public final void getName()
    {
        String finalmethod="value";
 
        System.out.println("finalmethod :"+finalmethod);
    }
}

Output

Download Source Code