final

Final Local Variable may not have been initialized in anonymous inner class

限于喜欢 提交于 2019-12-23 08:50:20
问题 Here is my code: final Foo myFoo = new Foo(new Inner() { @Override callback(){ myFoo.bar(); } }); (With actual function names) final MyArrayAdapter aa = new MyArrayAdapter(new View.OnClickListener() { @Override onClick(){ aa.notifyDataSetChanged(); } }); Java is giving me an error about how myFoo might not have been initialized. Is there any way to fix this? I can potentially set the callback to null when I construct the object and then change it afterwards, but I'd hope for there to be a

Why Java Inner Classes require variables of outer class be final? [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-23 05:35:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Java - inner class and local variables How does marking a variable as final allow inner classes to access them? Local Inner class can not only access the instance variables but local variables of the method (in which they are defined) as well, but the local varible has to be declared final . Why the local variables have to declared to be final in this case? 回答1: The reason is that an instance of the local inner

How can I assign final variables of the base class within a derived class' constructor in Java?

删除回忆录丶 提交于 2019-12-23 04:23:36
问题 I have a base Color class that looks something like this. The class is designed to be immutable, so as a result has final modifiers and no setters: public class Color { public static Color BLACK = new Color(0, 0, 0); public static Color RED = new Color(255, 0, 0); //... public static Color WHITE = new Color(255, 255, 255); protected final int _r; protected final int _g; protected final int _b; public Color(int r, int b, int g) { _r = normalize(r); _g = normalize(g); _b = normalize(b); }

Handling of `final` by the JVM

江枫思渺然 提交于 2019-12-22 18:06:24
问题 In a comment to this question, I'm claiming that final in some cases must be honored by the JVM. The safe publication of final variables has been already asked and so was the handling of static final members. But what about enforcing classes and methods being final and forbidding overwriting of final fields? IMHO this can and must be done at class loading time. My argument is that For the security of the JVM is crucial that e.g. String is final , so a hand-crafted class extending it must not

Why is the java.util.Scanner class declared 'final'?

被刻印的时光 ゝ 提交于 2019-12-22 06:38:07
问题 I use the Scanner class for reading multiple similar files. I would like to extend it to make sure they all use the same delimiter and I can also add methods like skipUntilYouFind(String thisHere) that all valid for them all. I can make a utility-class that contain them, or embed the Scanner Class as a variable inside another class but this is more cumbersome. I have found some reasons to declare a class final, but why is it done here? 回答1: Probably because extending it and overwriting some

Might not have been initialized error at null check

拜拜、爱过 提交于 2019-12-22 04:32:29
问题 I'm checking if the variable is initialized but at that point netbeans is giving me variable reader might not have been initialized warning. How do I fix/suppress this? This is my code (summary): final Reader reader; try { reader = new Reader(directory); //additional stuff that can cause an exception } catch (Exception ex) { //do stuff } finally { if (reader != null); } The point of the if check is to determine whether it is initialized. And what is the best practice for this? 回答1: If reader

final 修饰符

青春壹個敷衍的年華 提交于 2019-12-22 03:08:29
final 修饰符:不可改变的 它可以修饰非抽象类、非抽象方法和变量 1.修饰类: 不能被继承 没有子类 2.final修饰方法:该方法不能被覆盖 final getClass();//获得对象本质类型 3.final修饰变量: 该变量是常量,【只能赋一次值】,必须要赋初始值 。 非静态final成员变量: 赋值: 1,声明的同时赋值 2,可以在匿名代码块中赋值 3,可以在构造器中赋值 静态的final成员变量: 1,声明的同时赋值 2,可以在静态代码块中赋值 【String类不能被继承 因为String类有final修饰】 4final变量的赋值 4.1:final修饰非静态成员变量,必须要赋值,并且只能在下面三个地方赋值,赋值以后不能改。 4.1.1:声明的同时 赋值 4.1.2:匿名代码块内 赋值 4.1.3:类中任何构造器 赋值 4.2:final修饰static静态的成员变量 4.2.1:声明的同时 赋值 .4.2.2:static代码块内 赋值 4.3:final修饰变量 结论 4.3.1: final可以修饰任何变量. 4.3.1: final修饰的变量必须赋初始值 4.3.3: final修饰的变量被一次赋值以后不能跟改 来源: https://www.cnblogs.com/yxj808/p/11964852.html

Partial constructed objects in the Java Memory Model

只愿长相守 提交于 2019-12-21 20:34:36
问题 I came across the following code in an article somewhere on the Internet: public class MyInt { private int x; public MyInt(int y) { this.x = y; } public int getValue() { return this.x; } } The article states that Constructors are not treated special by the compiler (JIT, CPU etc) so it is allowed to reorder instructions from the constructor and instructions that come after the constructor. Also, this JSR-133 article about the Java Memory Model states that A thread that can only see a

Java Concurrency : Volatile vs final in “cascaded” variables?

牧云@^-^@ 提交于 2019-12-21 13:09:24
问题 is final Map<Integer,Map<String,Integer>> status = new ConcurrentHashMap<Integer, Map<String,Integer>>(); Map<Integer,Map<String,Integer>> statusInner = new ConcurrentHashMap<Integer, Map<String,Integer>>(); status.put(key,statusInner); the same as volatile Map<Integer,Map<String,Integer>> status = new ConcurrentHashMap<Integer, Map<String,Integer>>(); Map<Integer,Map<String,Integer>> statusInner = new ConcurrentHashMap<Integer, Map<String,Integer>>(); status.put(key,statusInner); in case the

Why are we allowed to have a final main method in java?

此生再无相见时 提交于 2019-12-21 09:27:06
问题 Can anyone tell me the use of making main method as final in java. while this is allowed in java public static final void main(String[] args) { } I dont see any use of making it final. anyways it is static so we can not override it. 回答1: Adding final to a static method can actually make a difference. Consider the following code: class A { public static void main(String[] args) { System.out.println("A"); } } class B extends A { public static void main(String[] args) { System.out.println("B");