xxxxxxxxxx
if a variable declared as final then no class can change its value once
it is given a value.
xxxxxxxxxx
Final is used to apply restrictions on class, method, and variable.
The final class can't be inherited, final method can't be overridden,
and final variable value can't be changed. Final is a keyword
xxxxxxxxxx
private final String hello = "Hello World!";
/*
The keyword final states that the variable, method or class
associated will not have it's value changed.
*/
xxxxxxxxxx
This simple explanation is to all those people who are overwhelmed by
the technical answer proveded by your mentors, google etc Have fun!!!
The key word : "final"
The final key word is used to declare a variable as constant
Can be implemented on classes, attributes methods
Example:
if i declare a variable like
{
int example = 10;
}
i can overwrite it by
{
example = 20;
}
now example is "20".
//===============================================================
final can be used to declare a class , attribute , method
as constant throught the program so that they cannot be OVERWRITTEN !!!
{
final int example = 10;
example = 20; //this will give an error.
}
xxxxxxxxxx
Final is used to apply restrictions on class, method, and variable.
The final class can't be inherited, final method can't be overridden,
and final variable value can't be changed. Final is a keyword
xxxxxxxxxx
// Value cannot be changed:
final double PI = 3.14;
Note that the variable must be given a value when it is declared as final. final variables cannot be changed; any attempts at doing so will result in an error message.
xxxxxxxxxx
You cannot extend a final class. If you try it gives you a compile time error.
xxxxxxxxxx
// create a final class
final class FinalClass {
public void display() {
System.out.println("This is a final method.");
}
}
// try to extend the final class
class Main extends FinalClass {
public void display() {
System.out.println("The final method is overridden.");
}
public static void main(String[] args) {
Main obj = new Main();
obj.display();
}
}