final

Unexpected Behavior wrt the final modifier

ⅰ亾dé卋堺 提交于 2019-12-24 00:58:25
问题 This is my code package alpha ; class A1 { static class A11 { private final // WHAT IS THE EFFECT OF THIS MODIFIER? void fun ( String caller ) { System . out . println ( "A11:\t" + caller ) ; } } static class A12 extends A11 { private void fun ( String caller ) { super . fun ( caller + caller ) ; } } public static void main ( String [ ] args ) { A12 a12 = new A12 ( ) ; a12 . fun ( "Hello" ) ; } } I have found that with or without the final mdifer in A1.A11 the program compiles and runs. I can

How to allow variables being set only once in java

荒凉一梦 提交于 2019-12-24 00:47:49
问题 I need to implement a class in java where its field needs to be set only once, but it's value is not known at field declaration. To do so I have: public class MyClass { private String field1 = null; public void setField1(String value) { if (field1 == null) { field1 = value; } } } However what I have do not prevent users to call setField1 more than once. That may lead to confusion where a user is trying to set it (without knowing it has been set somewhere else) and then the user will get

java final和static用法总结

亡梦爱人 提交于 2019-12-23 16:25:16
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> Java关键字final、static使用总结 一、final 根据程序上下文环境,Java关键字final有“这是无法改变的”或者“终态的”含义,它可以修饰非抽象类、非抽象类成员方法和变量。你可能出于两种理解而需要阻止改变:设计或效率。 final类不能被继承,没有子类,final类中的方法默认是final的。 final方法不能被子类的方法覆盖,但可以被继承。 final成员变量表示常量,只能被赋值一次,赋值后值不再改变。 final不能用于修饰构造方法。 注意:父类的private成员方法是不能被子类方法覆盖的,因此private类型的方法默认是final类型的。 1、final类 final类不能被继承,因此final类的成员方法没有机会被覆盖,默认都是final的。在设计类时候,如果这个类不需要有子类,类的实现细节不允许改变,并且确信这个类不会载被扩展,那么就设计为final类。 2、final方法 如果一个类不允许其子类覆盖某个方法,则可以把这个方法声明为final方法。 使用final方法的原因有二: 第一、把方法锁定,防止任何继承类修改它的意义和实现。 第二、高效。编译器在遇到调用final方法时候会转入内嵌机制,大大提高执行效率。 例如: public class Test1 { public

final methods are inlined?

女生的网名这么多〃 提交于 2019-12-23 12:12:57
问题 Are Java final methods automatically inlined? Many books says yes many books says no!!! 回答1: Interesting question, prompted me to look into it further. 2 interesting remarks I found - 1 comment that automatic inlining is a bug: Contrary to the implication of many tips, methods declared as final cannot be safely inlined by the compiler, because the method could have a non-final declaration at runtime. To see why, suppose the compiler looks at class A and subclass B, and sub-subclass C and sees

JMM guarantees about final as field and non final reference to the object

假如想象 提交于 2019-12-23 12:07:25
问题 I try to understand final fields semantic. Lets research code: public class App { final int[] data; static App instance; public App() { this.data = new int[]{1, 0}; this.data[1] = 2; } public static void main(String[] args) { new Thread(new Runnable() { public void run() { instance = new App(); } }).start(); while (instance == null) {/*NOP*/} System.out.println(Arrays.toString(instance.data)); } } I have some questions: Does jmm guarantee, that if application terminates then it output [1,2] ?

Difference between initialization of final class variable

回眸只為那壹抹淺笑 提交于 2019-12-23 11:24:44
问题 I just tried out to set a variable, showing me that the super constructor has been finished without wasting my code. So I remembered how class variables are initialized; after the super constructor but before the class constructor. But there is something strange if you look at this example: public class Init { public Init() { System.out.println("Init instance of " + this.getClass().getSimpleName()); System.out.println("syso Init:"); this.syso(false); } protected void syso(boolean childCall) {

Using final 1-element array for anonymous inner class

一个人想着一个人 提交于 2019-12-23 09:16:22
问题 I stumbled across this trick for getting a value from an anonymous inner class to a variable which is declared in the outer class. It works, but it feels like a dirty hack: private int showDialog() { final int[] myValue = new int[1]; JPanel panel = new JPanel(); final JDialog dialog = new JDialog(mainWindow, "Hit the button", true); dialog.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE ); JButton button = new JButton("Hit me!"); button.addActionListener(new ActionListener() {

Why are local variables not declared final in most open source java projects?

二次信任 提交于 2019-12-23 09:09:08
问题 If I look at the java source source code in the OpenJDK or Hibernate or Apache I have yet to see any local variables declared final. This suggests that the developers of some of the most widely used java software libraries: do not believe the final keyword improves readablity. do not believe it significantly improves performance. Why do the majority of contrbuters on stackoverflow believe it it should be used (based on the highest voted responses)? 回答1: do not believe the final keyword

Variable 'final' modifier lost in Bytecode?

我的梦境 提交于 2019-12-23 08:57:08
问题 Analyzing the bytecode of this simple class, I have come to the conclusion that the compiler doesn't retain any information about a local variable being final . This seems weird though, since I believe the HotSpot compiler could actually use this information to do optimizations. Code : public static void main(String[] args) { final int i = 10; System.out.println(i); } Bytecode : public static void main(java.lang.String[]); descriptor: ([Ljava/lang/String;)V flags: ACC_PUBLIC, ACC_STATIC Code:

variable r might not have been initialized

二次信任 提交于 2019-12-23 08:54:23
问题 There's a very simple program: public class A { public static void main(String[] p) { final Runnable r = new Runnable() { public void run() { System.out.println(r); } }; r.run(); } } And this gives: $ javac A.java A.java:6: variable r might not have been initialized System.out.println(r); ^ 1 error Why? How can a Runnable reference a variable pointing to it? (In the real code, there is one more level (a listener), and referencing via this does not work) 回答1: In this case, you can use "this"