Welcome Guest | Sign in | Register
Inner Classes - Discussion Page For Q.3031 | 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 ?

public class Outer {
private int a = 7;

class Inner {
public void displayValue() {
System.out.println("Value of a is " + a);
}
}
}


public class Test {

public static void main(String... args) throws Exception {
Outer mo = new Outer();
Outer.Inner inner = mo.new Inner();
inner.displayValue();

}

}


A. Value of a is 7 B. Compile Error - not able to access private member.
C. Runtime Exception D. Value of a is 8

Answer and Explanation

Answer:Value of a is 7

Explanation:
An inner class instance can never stand alone without a direct relationship to an instance of the outer class.
you can access the inner class is through a live instance of the outer class.
Inner class can access private member of the outer class.

Comment on this Question

Name:

Loading...




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