Final keyword
In General, the meaning of Final is "can not be changed" or "Constant". In this sections discuss the three places where final can be used: for data, methods and for a class.
The advantages of using the final keyword are
1. Other programmers know that this variable shouldn't be changed or assigned another value.
2. It stops people overriding classes when you don't want them to or they shouldn't.
3. It helps stop accidentally assigning the wrong value bugs e.g. typos etc.
4. It stops you accidentally changing the value.
5. A visible advantage of declaring a java variable as static final is, the compiled java class results in faster performance.
Final classes
The class declared as final can't be subclass or extend. This is done for reasons of security and efficiency. Accordingly, many of the Java standard library classes are final, for example java.lang.System and java.lang.String. All methods in a final class are implicitly final.
public
final class MyChessClass
{
...
public void set(ChessPiece pieceMoved, BoardLocation newLocation)
{
...
}
...
}
For Example
final class Demo
{
private int type=300;
public int getType() {return type;}
}
public class MyFinalClass
{
public static void main(String args[])
{
int x = 5000;
int i = x;
int t = 0;
Demo aref = new Demo ();
while (i-- > 0)
t = aref.getType();
System.out.println("value of t="+t);
}
}
This program run successfully and print the value of t=300.
If you extends the demo class in MyFinalClass, It will generate Error Message. Suppose we try to do it like below code.
final class Demo
{
private int type=300;
public int getType() {return type;}
}
public class MyFinalClass extends Demo
{
public static void main(String args[])
{
int x = 5000;
int i = x;
int t = 0;
Demo aref = new Demo ();
while (i-- > 0)
t = aref.getType();
System.out.println("value of t="+t);
}
}
Output
Note:- You can also declare an entire class final this prevents the class from being subclassed. This is particularly useful, for example, when creating an immutable class like the string class.
Final Methods
There are two reasons to make a method final.
- The First reason is to prevent any inheriting class from changing its meaning.
- The second reason for final methods is efficiency.
In first case: A final method is implemented exactly once. A final method cannot be overridden by subclasses. This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.
In second case ,If you make a method or class final, you are allowing the compiler to turn any calls to that method into inline calls. This is not completely true; the compiler is unable to do this because the classes are loaded at runtime and might not be the same version as the ones that were just compiled.
The final method declared as follows :
final
void myFinalMethod(ChessPiece pieceMoved,
BoardLocation newLocation)
{
...
}
Note: Any private methods in a class are implicitly final. Because you can't access a private method, you can't override it (the compiler gives an error message if you try). You can add the final specifier to a private method but it doesn't give that method any extra meaning.
Final Variables and method Parameters
A final variable can be set only once, allowing you to declare local constants. A constant is useful for two reasons:-
- It can be a compile-time constant that won't ever change.
- It can be a value initialized at run-time that you don't want change.
We can initialized final variable either via an initializer or an assignment statement. A field that is both static and final has only one piece of storage that cannot be changed.
When you used final with primitives ,final makes the value a constant. For Example.
When you used final with object handles the meaning gets a bit confusing. Final makes the handle a constant. The handle must be initialized to an object at the time of declaration, and the handle can never be changed to point of another object. However , the object can be modified; But Java does not provide a way to make any arbitrary object a constant.
Java 1.1 allows you to A parameter to a function can be declared with the keyword final. This indicates that the parameter cannot be modified in the function.
Final argument figure:-
Blank Final
The blank finals, which was introduced in Java 1.1, is a final variable whose declaration lacks an initializer. Declaring a variable with the final keyword makes it is not possible to reinitialize that variable once it has been initialized with an explicit value ( notice we said explicit rather than default). A blank final variable forces the constructors to initialise it.
Example:-
public class BlankFinalDemo
{
final int x;
{
System.out.println(getIntValue());
}
private int getIntValue() {
return x;
}
public BlankFinalDemo(){
x=20;
System.out.println(x);
}
public static void main(String arg[]){
new BlankFinalDemo();
}
}
Output:
Note:- If you define data member in interface then these data member are by default public static and final.
For Example
interface Demo
{
int x=10;
}
//1st way to prove that by default data member of an interface are public static and final
public class Child implements Demo
{
public static void main(String[] args)
{
System.out.println("The value of x="+x);
}
}
//or
//2st way to prove that by default data member of an interface are public static and final
class Child1
{
public static void main(String[] args)
{
System.out.println("The value of x="+Demo.x);
}
}
Output: