Programming

Java – Use of Final Keyword in Java

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 the superclass. The final keyword is used for the 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(){
.........
.........
}
.
.
}

Tuts

About Author

Tutsmaster.org provides tutorials related to tech and programmings. We are also setting up a community for the users and students.