In Java, final keyword is applied in various context. The final keyword is a modifier means the final class can't be extended, a variable can't be modified, and also a method can't be override.
The class declared as final can't be subclass or extend. The final class declared as follows :
public final class MyFinalClass
The final method can be declare as follows:
public final String convertCurrency()
The final method can't be override in a subclass.
The field declared as final behaves like constant. Means once it is declared, it can't be changed. Before compiling,only once it can be set; after that you can't change it's value. Attempt to change in it's value lead to exception or compile time error.
You can declare the final fields as :
public final double radius = 126.45;
public final int PI = 3.145;
The fields which are declared as static, final and public are known as named constants. Given below a example of named constant :
public class Maths{
public static final double x = 2.998E8;
}
You can also declare method's argument as final. The final argument can't be modified by the method directly.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Final Methods - Java Tutorials
Post your Comment