Java Programming - Declarations and Access Control - Discussion

Discussion Forum : Declarations and Access Control - General Questions (Q.No. 2)
2.
public class Outer 
{ 
    public void someOuterMethod() 
    {
        //Line 5 
    } 
    public class Inner { } 
    
    public static void main(String[] argv) 
    {
        Outer ot = new Outer(); 
        //Line 10
    } 
} 

Which of the following code fragments inserted, will allow to compile?

new Inner(); //At line 5
new Inner(); //At line 10
new ot.Inner(); //At line 10
new Outer.Inner(); //At line 10
Answer: Option
Explanation:

Option A compiles without problem.

Option B gives error - non-static variable cannot be referenced from a static context.

Option C package ot does not exist.

Option D gives error - non-static variable cannot be referenced from a static context.

Discussion:
14 comments Page 2 of 2.

Shiva said:   1 decade ago
Can we create obj for inner class in outer class?

Parth said:   1 decade ago
What is the effect of new Inner(); in the code ?

Alex said:   1 decade ago
simply new Outer(); //At line 5 will also compile

while new ot.Inner(); or new or.Outer(); //At line 5 or 10 will give same error- package ot doesn't exist

Alex said:   1 decade ago
new Outer.Inner(); //At line 5 will compile without any error too


Post your comments here:

Your comments will be displayed after verification.