当前位置: 技术问答>java相关
一道关于内部类的SCJP题?请帮忙解释一下
来源: 互联网 发布时间:2015-03-01
本文导语: Given the following code public class FinAc{ static int l = 4; private int k=2; public static void main(String argv[]){ FinAc a = new FinAc(); a.amethod(); } public void ame...
Given the following code
public class FinAc{
static int l = 4;
private int k=2;
public static void main(String argv[]){
FinAc a = new FinAc();
a.amethod();
}
public void amethod(){
final int i = 99;
int j = 6;
class CInMet{
public void mymethod(int q){
//Here
}//end of mymethod
}//End of CInMet
CInMet c = new CInMet();
c.mymethod(i);
}//End of amthod
}
Which of the following variables are visible on the line marked with the comment //Here?
1) l
2) k
3) i
4) j
public class FinAc{
static int l = 4;
private int k=2;
public static void main(String argv[]){
FinAc a = new FinAc();
a.amethod();
}
public void amethod(){
final int i = 99;
int j = 6;
class CInMet{
public void mymethod(int q){
//Here
}//end of mymethod
}//End of CInMet
CInMet c = new CInMet();
c.mymethod(i);
}//End of amthod
}
Which of the following variables are visible on the line marked with the comment //Here?
1) l
2) k
3) i
4) j
|
l,k,i
Local classes Can access all the features of the enclosing class (because they are defined inside the method of the class) but can access only final variables defined inside the method (including method arguments). This is because the class can outlive the method, but the method local variables will go out of scope – in case of final variables, compiler makes a copy of those variables to be used by the class.
Local classes Can access all the features of the enclosing class (because they are defined inside the method of the class) but can access only final variables defined inside the method (including method arguments). This is because the class can outlive the method, but the method local variables will go out of scope – in case of final variables, compiler makes a copy of those variables to be used by the class.
|
l,k,,i
|
我认为是: l,k
请公布正确答案!
请公布正确答案!
|
l,k,i
一个method的final的local field,在该method内的内部类(local class)
中是visible的,否则不是visible的,嘻嘻
一个method的final的local field,在该method内的内部类(local class)
中是visible的,否则不是visible的,嘻嘻
|
因为j不是final的,所以不能用于local class,语法上是这样规定的,
具体原因不知道,嘻嘻
具体原因不知道,嘻嘻