Wednesday, August 11, 2010

Static Nested Classes

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 8, Question 1

Which are true about a static nested class? (Choose all that apply.)
  1. You must have a reference to an instance of the enclosing class in order to instantiate it


    Incorrect because static nested classes do not need (and can’t use) a reference to an instance of the enclosing class.

  2. It does not have access to non-static members of the enclosing class


    Correct because a static nested class is not tied to an instance of the enclosing class, and thus can’t access the non-static members of the class (just as a static method can’t access non-static members of a class).

  3. Its variables and methods must be static


    Incorrect because static nested classes can declare and define non-static members.

  4. If the outer class is named MyOuter, and the nested class is named MyInner, it can be instantiated using new MyOuter.MyInner();


    Correct, it uses the correct syntax for instantiating a static nested class.

  5. It must extend the enclosing class


    Wrong because...it just is. There’s no rule that says an inner or nested class has to extend anything.

No comments:

Post a Comment