Home / Java Programming / Declarations and Access Control :: Discussion

Discussion :: Declarations and Access Control

  1.  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?

  2. A.

    new Inner(); //At line 5

    B.

    new Inner(); //At line 10

    C.

    new ot.Inner(); //At line 10

    D.

    new Outer.Inner(); //At line 10

    View Answer

    Workspace

    Answer : Option A

    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.


Be The First To Comment