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.
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.
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.
Important points on final in Java:
list.add(“home loan”); //valid
list.add("personal loan"); //valid
loans = new Vector(); //not valid
Note: We cannot make an ‘abstract’ class or method as ‘final’ because an ‘abstract’ class needs to be extended which will not be possible if you mark it as ‘final’.
Questions and answers:
What happens when you use ‘final’ for a class declaration?
A: You cannot have a sub class of a class declared final i.e. No other class can extend this class.
What happens when you declare a method as final?
A: Simply put, the method cannot be overridden.
Can you override a method(Not declared as ‘final’) present inside a final class?
A: Obviously Not. Since there’s no way to extend a class declared final there’s no way of overriding its methods!
What happens when I declare a method inside a class as both ‘private’ and ‘final’ ?
A: Making a class both ‘private’ and ‘final’ makes the ‘final’ keyword redundant as a private method cannot be accessed in its sub class. But hey, you’ll be able to declare a method of the same name as in the base class if the method has been made private in the base class. But then that doesn’t mean you’re overriding the method. You’re simply declaring a new method in the sub class.
Example:
*******************************************************
import java.util.ArrayList;
/**
* We can not inherit this class as it is final class
*
*/
public final class FinalKeywordExample {
/**
* blank static final variable. This can be initialized only inside
* constructors.
*/
final int p;// blank final variable
/**
* blank static final variable. This can be initialized only inside static
* blocks.
*/
final static int q;
final int r = 8;// final variable
/**
* Here we assign arraylist reference as final. So only reference can not
* be changed but you can add, remove or change object inside
* collection
*/
final ArrayList<String> arrayList = new ArrayList<String>();
/**
* We can not override this method as it is final method.
*/
public final void finalMethod() {
final int g = 11; System.out.println("this is final method and local final variable value is " + g);
// we can not initialize blank final variable in side methods. So
// commented it.
// q = 9;
// we can not initialize blank static final variable in side methods.
// So commented it.
// p = 10;
}
public FinalKeywordExample() {
p = 10;
// q = 9;
System.out.println("final variable p value is" + " " + p);
}
static {
q = 9;
System.out.println("final variable q value is " + " " + q);
}
public static void main(String a[]) {
// p = 10;// we can initialize blank final variables only in
// constructors
FinalKeywordExample example = new FinalKeywordExample();
System.out.println("Final variable r value is " + " " + example.r);
// We can not modify the r value so following line causes compile
// time error. so it is commented.
// example.r = 30;
example.finalMethod();
example.arrayList.add("Java");
example.arrayList.add("Jsp");
example.arrayList.add("Servlets");
example.arrayList.add("Struts");
System.out.println("Array list values are " + example.arrayList);
// Following line causes error because arraylist reference is final
// type. So it is commented.
//example.arrayList = new ArrayList<String>();
}
}
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.
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.
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.
Important points on final in Java:
- Java classes declared as final cannot be extended. Restricting inheritance.
- Methods declared as final cannot be overridden.
- A variable that is declared as final and not initialized is called a blank final variable.The blank final variable can be initialized in constructor only. The blank final variable can be static also which will be initialized in static block only.
- Final keyword can be applied to member variable, local variable, method or class in Java.
- Final member variable must be initialized at the time of declaration or inside constructor, failure to do so will result in compilation error.
- You can not reassign value to final variable in Java.
- Local final variable must be initializing during declaration.
- Only final variable is accessible inside anonymous class in Java.
- Final method can not be overridden in Java.
- Final class can not be inheritable in Java.
- Final is different than finally keyword which is used on Exception handling in Java.
- Final should not be confused with finalize() method which is declared in object class and called before an object is garbage collected by JVM.
- All variable declared inside java interface are implicitly final.
- Final and abstract are two opposite keyword and a final class can not be abstract in java.
- Final methods are bonded during compile time also called static binding.
- 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".
- Making a class, method or variable final in Java helps to improve performance because JVM gets an opportunity to make assumption and optimization.
- As per Java code convention final variables are treated as constant and written in all Caps e.g.
- 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:
list.add(“home loan”); //valid
list.add("personal loan"); //valid
loans = new Vector(); //not valid
Note: We cannot make an ‘abstract’ class or method as ‘final’ because an ‘abstract’ class needs to be extended which will not be possible if you mark it as ‘final’.
Questions and answers:
What happens when you use ‘final’ for a class declaration?
A: You cannot have a sub class of a class declared final i.e. No other class can extend this class.
What happens when you declare a method as final?
A: Simply put, the method cannot be overridden.
Can you override a method(Not declared as ‘final’) present inside a final class?
A: Obviously Not. Since there’s no way to extend a class declared final there’s no way of overriding its methods!
What happens when I declare a method inside a class as both ‘private’ and ‘final’ ?
A: Making a class both ‘private’ and ‘final’ makes the ‘final’ keyword redundant as a private method cannot be accessed in its sub class. But hey, you’ll be able to declare a method of the same name as in the base class if the method has been made private in the base class. But then that doesn’t mean you’re overriding the method. You’re simply declaring a new method in the sub class.
Example:
*******************************************************
import java.util.ArrayList;
/**
* We can not inherit this class as it is final class
*
*/
public final class FinalKeywordExample {
/**
* blank static final variable. This can be initialized only inside
* constructors.
*/
final int p;// blank final variable
/**
* blank static final variable. This can be initialized only inside static
* blocks.
*/
final static int q;
final int r = 8;// final variable
/**
* Here we assign arraylist reference as final. So only reference can not
* be changed but you can add, remove or change object inside
* collection
*/
final ArrayList<String> arrayList = new ArrayList<String>();
/**
* We can not override this method as it is final method.
*/
public final void finalMethod() {
final int g = 11; System.out.println("this is final method and local final variable value is " + g);
// we can not initialize blank final variable in side methods. So
// commented it.
// q = 9;
// we can not initialize blank static final variable in side methods.
// So commented it.
// p = 10;
}
public FinalKeywordExample() {
p = 10;
// q = 9;
System.out.println("final variable p value is" + " " + p);
}
static {
q = 9;
System.out.println("final variable q value is " + " " + q);
}
public static void main(String a[]) {
// p = 10;// we can initialize blank final variables only in
// constructors
FinalKeywordExample example = new FinalKeywordExample();
System.out.println("Final variable r value is " + " " + example.r);
// We can not modify the r value so following line causes compile
// time error. so it is commented.
// example.r = 30;
example.finalMethod();
example.arrayList.add("Java");
example.arrayList.add("Jsp");
example.arrayList.add("Servlets");
example.arrayList.add("Struts");
System.out.println("Array list values are " + example.arrayList);
// Following line causes error because arraylist reference is final
// type. So it is commented.
//example.arrayList = new ArrayList<String>();
}
}
****************************************************
Final keyword in java
ReplyDeleteFinal keyword is mainly used at three places in java; at variable level to make a variable as a constant, at method level to Restrict method overriding, at class level to Restrict inheritance.