Final in java is very important keyword and can be applied to class, method, and variables in Java. In this java final tutorial we will see what is final keyword in Java, what does it mean by making final variable, final method and final class in java and what are primary benefits of using final keywords in Java and finally some examples of final in Java. Final is often used along with static keyword in Java to make static final constant and you will see how final in Java can increase performance of Java application.
Example of Final variable, Final method and Class in Java
What is final keyword in Java?
Final is a keyword
or reserved word in java and can be applied to member variables,
methods, class and local variables in Java. Once you make a reference
final you are not allowed to change that reference and compiler will
verify this and raise compilation error if you try to re-initialized final variables in java.
What is final variable in Java?
Any variable either member variable or local variable (declared
inside method or block) modified by final keyword is called final
variable. Final variables are often declare with static keyword in java
and treated as constant. Here is an example of final variable in Java
public static final String LOAN = "loan";
LOAN = new String("loan") //invalid compilation error
Final variables are by default read-only.
What is final method in Java
Final
keyword in java can also be applied to methods. A java method with
final keyword is called final method and it can not be overridden in
sub-class. You should make a method final in java if you think it’s
complete and its behavior should remain constant in sub-classes. Final
methods are faster than non-final methods because they are not required
to be resolved during run-time and they are bonded on compile time. Here
is an example of final method in Java:
class PersonalLoan{
public final String getName(){
return "personal loan";
}
}
class CheapPersonalLoan extends PersonalLoan{
@Override
public final String getName(){
return "cheap personal loan"; //compilation error: overridden method is final
}
}
What is final Class in Java
Java class with final modifier is called final class in Java.
Final class is complete in nature and can not be sub-classed or
inherited. Several classes in Java are final e.g. String, Integer and
other wrapper classes. Here is an example of final class in java
final class PersonalLoan{
}
class CheapPersonalLoan extends PersonalLoan{ //compilation error: cannot inherit from final class
}
Benefits of final keyword in Java
Here are few benefits or advantage of using final keyword in Java:
1. Final keyword improves performance. Not just JVM can cache final variable but also application can cache frequently use final variables.
2. Final variables are safe to share in multi-threading environment without additional synchronization overhead.
3. Final keyword allows JVM to optimized method, variable or class.
Final and Immutable Class in Java
Final
keyword helps to write immutable class. Immutable classes are the one
which can not be modified once it gets created and String is primary
example of immutable and final class which I have discussed in detail on
Why String is final or immutable in Java.
Immutable classes offer several benefits one of them is that they are
effectively read-only and can be safely shared in between multiple
threads without any synchronization overhead. You can not make a class
immutable without making it final and hence final keyword is required to
make a class immutable in java.
Example of Final in Java
Java
has several system classes in JDK which are final, some example of
final classes are String, Integer, Double and other wrapper classes. You
can also use final keyword to make your code better whenever it
required. See relevant section of java final tutorial for example of final variable, final method and final class in Java.
Important points on final in Java
1. Final keyword can be applied to member variable, local variable, method or class in Java.
2. Final member variable must be initialized at the time of declaration or inside constructor, failure to do so will result in compilation error.
3. You can not reassign value to final variable in Java.
4. Local final variable must be initializing during declaration.
5. Only final variable is accessible inside anonymous class in Java.
6. Final method can not be overridden in Java.
7. Final class can not be inheritable in Java.
8. Final is different than finally keyword which is used on Exception handling in Java.
9. Final should not be confused with finalize() method which is declared in object class and called before an object is garbage collected by JVM.
10. All variable declared inside java interface are implicitly final.
11. Final and abstract are two opposite keyword and a final class can not be abstract in java.
12. Final methods are bonded during compile time also called static binding.
13. Final variables which is not initialized during declaration
are called blank final variable and must be initialized on all
constructor either explicitly or by calling this(). Failure to do so
compiler will complain as "final variable (name) might not be initialized".
14.
Making a class, method or variable final in Java helps to improve
performance because JVM gets an opportunity to make assumption and
optimization.
15. As per Java code convention final variables are treated as constant and written in all Caps e.g.
private final int COUNT=10;
16.
Making a collection reference variable final means only reference can
not be changed but you can add, remove or change object inside
collection. For example:
private final List Loans = new ArrayList();
list.add(“home loan”); //valid
list.add("personal loan"); //valid
loans = new Vector(); //not valid
That’s all on final in Java. We have seen what final variable, final method is and final class in Java and what does those mean. In Summary whenever possible start using final in java it would result in better and faster code.
·
A java variable can be declared using the keyword final. Then the
final variable can be assigned only once.
·
A variable that is declared as final and not initialized is called
a blank final variable. A blank final variable forces the constructors to
initialise it.
·
Java classes declared as final cannot be extended. Restricting
inheritance!
·
Methods declared as final cannot be overridden. In methods private
is equal to final, but in variables it is not.
·
final parameters – values of the parameters cannot be changed
after initialization. Do a small java exercise to find out the implications of
final parameters in method overriding.
·
Java local classes can only reference local variables and
parameters that are declared as final.
·
A visible advantage of declaring a java variable as static final
is, the compiled java class results in faster performance.
A discussion inviting
controversy on java final keyword:
‘final’ should not be called as constants. Because when an array
is declared as final, the state of the object stored in the array can be
modified. You need to make it immutable in order not to allow modifcations. In
general context constants will not allow to modify. In C++, an array declared
as const will not allow the above scenario but java allows. So java’s final is
not the general constant used across in computer languages.
A variable that is declared static final is closer to constants in
general software terminology. You must instantiate the variable when you
declare it static final.
Definition as per java language specification (third edition) –
4.12.4 is “A final variable may only be assigned to once.”(§4.1.2)
Java language specification tries to redefine the meaning of
constant in the following way!
We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).
We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28) a constant variable. Whether a variable is a constant variable or not may have implications with respect to class initialization (§12.4.1), binary compatibility (§13.1, §13.4.9) and definite assignment (§16).
No comments:
Post a Comment