Final Keyword In Java
2016-01-23 02:20
585 查看
Introduction
final keyword can be used along with variables, methods and classes.- variable
- method
- class
Mnemonic
Afinal classcannot be extended by any other class
A
final variablecannot be reassigned to another value
A
final methodcannot be overridden
Variable
final variable
We cannot change the value of a final variable once it is initialized.class Student{ final int studentID = 123; public void setID(){ studentID = 456; //error } final static int numberOfStuent = 345; static void setNumberOfStudent(){ numberOfStuent = 789; //error } }
blank final instance variable
A final instance variable that is not initialized at the time of declaration is known as blank final variable.We must initialize the blank final instance variable in constructor of the class otherwise a compile-time error occurs.. (only initialize in constructor)
class Student{ final int studentID; Student(){ studentID = 123; studentID = 456; //error } }
blank final class variable
A static final variable that is not initialized at the time of declaration is known as static blank final class variable. It can be initialized only in static block.class Student{ final static numberOfStuent; static{ numberOfStudent = 789; numberOfStuednt = 999; //error } }
final parameter
If you declare any parameter as final, you cannot change the value of it.class Calculate( public static int increase(final int a){ return ++a; //error } )
Q) Whats the use of blank final variable?
Lets say we have a Student class which is having a field called Roll No. Since Roll No should not be changed once the student is registered, we can declare it as a final variable in a class but we cannot initialize roll no in advance for all the students(otherwise all students would be having same roll no)class Student{ final int ROLL_NO; Student(int ROLL_NO){ this.ROLL_NO = ROLL_NO; } }
Method
final method
A final method cannot be overridden.The Counter is good example:
public class Counter{ private int counter = 0; public final int count(){ return counter++; } public final int reset(){ return (counter = 0); } }
If the
public final int count()method is not
final,we can do something like this:
Counter c = new Counter(){ public int count(){ super.count(); return super.count(); } } c.count();// not count2
Or something like this:
Counter c = new Counter(){ public int count(){ int lastCount = 0; for(int i = super.count(); --i >= 0;){ lastCount = super.count(); } return lastCount; } } c.count() // Now double count
Q) Is final method inherited?
Yes, final method is inherited but you cannot override.Class
final class
Final classis complete in nature and can not be subclassed or inherited. Several classes in Java ar e final e.g. String, Integer and other wrapper classes.
Benefits of final keyword in Java
Final keyword improves performance.Final variable are safe to share in multi-threading environment without additional synchronisation overhead.
Fianl keyword allows JVM to optimised method, variable or class.
Reference
Javafinalmethod: what does it promise?
Final Keyword In Java by javatpoint
Final Keyword In Java – Final variable, Method and Class by Beginners books
Why must a final variable be initialized before constructor completes?
What is final in Java? Final variable, Method, and Class Example
use of final class in java
相关文章推荐
- Java 程序代码书写风格及一些简单的注意事项 (
- Ubuntu 14 配置java环境
- Quartz定时任务使用配置
- Java VM
- java基础
- 懒惰模式下正则表达式Java运行与Regex Buddy运行结果不同的原因
- Java设计模式(一)----单例模式
- java 常用正则表达式 的使用
- java.lang.StackOverflowError
- 浅析JDK动态代理的源码
- java的左移和循环左移的实现(右类似)
- 4.Java基础复习--Set
- springmvc
- springmvc
- springmvc-框架提供的三种处理器映射组件
- 使用Eclipse-Maven-git做Java开发(15)--git分支和标签的使用
- Shiro简介
- jps工具
- JDK内置工具使用
- JDK内置工具-jstack-查看进程的状态以及异常诊断