I'm confused about the inner class being declared private. Here's the question:
What will happen when you attempt to compile and run this program:
public class Outer
{
public
String name = "Outer";
public static void main(String argv[])
{
Inner i = new Inner();
i.showName();
}//End of main
private class Inner
{
String name =new String("Inner");
void showName()
{
System.out.println(name);
}
}//End of Inner class
}
1) Compile and run with output of "Outer"
2) Compile and run with output of "Inner"
3) Compile time error because Inner is declared as private
4) Compile time error because of the line creating the instance of Inner
The correct answer is option 4. I thought that the program would also generate a compile time error because Inner is declared as private, but it seems to work fine. Why is this the case?
Pls help.
Thanks.
Mansi