i******e 发帖数: 1277 | 1 G家面试时被问到的,当时没答出来。
keyword "final" 在什么时候是required?也就是说,如果不加这个"final",编译不
会通过? |
x***n 发帖数: 464 | 2 在Interface中定义variables的时候。 |
c******n 发帖数: 710 | 3 这个不是implicit的吗
【在 x***n 的大作中提到】 : 在Interface中定义variables的时候。
|
F****n 发帖数: 3271 | 4 Please post to Java board
【在 i******e 的大作中提到】 : G家面试时被问到的,当时没答出来。 : keyword "final" 在什么时候是required?也就是说,如果不加这个"final",编译不 : 会通过?
|
c******e 发帖数: 73 | 5 variable need to be accessed by the method in inner class. |
c******e 发帖数: 73 | 6 variable need to be accessed by the method in inner class. |
l***o 发帖数: 208 | 7 can we have an example?
【在 c******e 的大作中提到】 : variable need to be accessed by the method in inner class.
|
h**********d 发帖数: 4313 | 8 有一个叫 method-local inner class 的东西
the inner class object cannot use the local variables of the method the inner class is in.
class MyOuter2 {
private String x = "Outer2";
void doStuff() {
String z = "local variable";
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Local variable z is " + z); // Won't Compile!
} // close inner class method
} // close inner class definition
} // close outer class method doStuff()
} // close outer class
Compiling the preceding code really upsets the compiler:
MyOuter2.java:8: local variable z is accessed from within inner class;
needs to be declared final
System.out.println("Local variable z is " + z);
^
Marking the local variable z as final fixes the problem:
final String z = "local variable";
// Now inner object can use it
【在 l***o 的大作中提到】 : can we have an example?
|