戳穿一个神话: form: ibm developerWorks Like many myths about Java performance, the erroneous belief that declaring classes or methods as final results in better performance is widely held but rarely examined. The argument goes that declaring a method or class as final means that the compiler can inline method calls more aggressively, because it knows that at run time this is definitely the version of the method that's going to be called. But this is simply not true. Just because class X is compiled against final class Y doesn't mean that the same version of class Y will be loaded at run time. So the compiler cannot inline such cross-class method calls safely, final or not. Only if a method is private can the compiler inline it freely, and in that case, the final keyword would be redundant.
to redex(cc): "But this is simply not true. Just because class X is compiled against final class Y doesn't mean that the same version of class Y will be loaded at run time. So the compiler cannot inline such cross-class method calls safely, final or not. Only if a method is private can the compiler inline it freely, and in that case, the final keyword would be redundant."----------yes,a class is just a reference,if declared as final it can't be inherited.But inside the class which was composed into another class can we change the value of the fields or the function of the methods?If we can,dose "final" just means that "me can't change,but inside me you can do anything properly"?
难道你对java机制有了新的突破???
final方法不能被 覆盖 任何一个final方法都是这样
被声明为final的变量必须在声明时给定初值,而在以后的引用中只能读取,不可修改.
被声明为final的方法也同样只能使用,不能重载.
form: ibm developerWorks
Like many myths about Java performance, the erroneous belief that declaring classes or methods as final results in better performance is widely held but rarely examined. The argument goes that declaring a method or class as final means that the compiler can inline method calls more aggressively, because it knows that at run time this is definitely the version of the method that's going to be called. But this is simply not true. Just because class X is compiled against final class Y doesn't mean that the same version of class Y will be loaded at run time. So the compiler cannot inline such cross-class method calls safely, final or not. Only if a method is private can the compiler inline it freely, and in that case, the final keyword would be redundant.
final的类变量在声明时可以不设定值,而在构造方法中设定初始值.
如果父类的方法不声明成final,子类覆盖后不也是调用子类的方法吗?和不声明成final不一样吗?高手指教!
在子类中如果定义了同名的方法,并且参数相同,当然是重载了父类的方法。final的作用是使父类的方法不能被重载,就是说如果你想去重载它就会报编译期错误。