Welcome Guest | Sign in | Register
Inner Classes - Discussion Page For Q.3036 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > Inner Classes > Questions and Answers

Q: What is the output for the below code ?
class Outer {
private String x = "Outer variable";
void doStuff() {
final String z = "local variable";
class Inner {
public void seeOuter() {
System.out.println("Outer x is " + x);
System.out.println("Local variable z is " + z);
}
}
Inner mi = new Inner();
mi.seeOuter();
}
public static void main(String... args){
Outer out = new Outer();
out.doStuff();
}
}
What is the output?
A. Compile Fail : local variable z is can't access from within inner class B. Outer x is Outer variable Local variable z is local variable
C. Outer x is Outer variable D. Compilation succeed but Runtime Exception

Answer and Explanation

Answer:Outer x is Outer variable Local variable z is local variable

Explanation:
the inner class object cannot use the local variables of the method the inner class is in.
only final can be accessible.

Comment on this Question

Name:

Loading...




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.