Java – Use of Final Keyword in Java

0
7
digitalample eye
If you declared Java variable is final, then that variable cannot re-declared that declaration for the single time. Means that variable can be initialized only for a single time.

 If Java classes are declared final, then those Java classes cannot be further extended. It means you cannot inherit the data from super class. The final keyword is used for following purposes in Java.

  1. Preventing a class from being inherited by another class.
  2. Declaring a constant variable
  3. Preventing a method from being overridden.
the following syntax is used to declare the final class, 
public final class XYZ{
Statements1;
Statements2;
Statements3;
.
.
}

the following syntax is used to declare the final variable,

public final class XYZ{
public final int PI=3.14;
public final float I=2.14F;
.
.
}

the following syntax is used to declare the final method,

public final class XYZ{
public final void display(){
.........
.........
}
.
.
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here