Machine-translated by Google. Questions are authored in English.
Top

Discussion

public class MyOuter 
{
    public static class MyInner 
    {
        public static void foo() { }
    }
}

which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class? 

  • A.MyOuter.MyInner m = new MyOuter.MyInner();
  • B.MyOuter.MyInner mi = new MyInner();
  • C.MyOuter m = new MyOuter();
    MyOuter.MyInner mi = m.new MyOuter.MyInner();
  • D.MyInner mi = new MyOuter.MyInner();

Answer: A

MyInner is a static nested class, so it must be instantiated using the fully-scoped name of MyOuter.MyInner.

Option B is incorrect because it doesn't use the enclosing name in the new.

Option C is incorrect because it uses incorrect syntax. When you instantiate a nested class by invoking new on an instance of the enclosing class, you do not use the enclosing name. The difference between Option A and C is that Option C is calling new on an instance of the enclosing class rather than just new by itself.

Option D is incorrect because it doesn't use the enclosing class name in the variable declaration.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.