Tuesday, 21 January 2014


In Java class can be declared as final using final keyword. If the final keyword is used in the class declaration the class becomes unable to be sub-classed. No any class can extend the final class i.e. features of a final class can't be inherited. Various classes of Java library are defined as final fro example java.lang.String, java.lang.System etc. If a final class contains the methods then these methods becomes final implicitly. In Java classes are declared as final for the security and efficiency reason. final makes the class immutable.

Syntax for declaring a class as final:

public final class FinalClassName

Example for using final keyword:

public class FinalVariableDemo
        final int number=10;
public void showFinalValue()
        System.out.println("Final vbariable value : "+number);

public static void main(String[] args) {
        FinalVariableDemo obVariableDemo=new FinalVariableDemo();


Final variable value : 10

No comments :

Post a Comment