7 Nested Classes and Interfaces



The code will compile without error and will print 123 when run. An instance of the Outer outer will be created and the field secret will be initialized to 123. A call to the createInner() method will return a reference to a newly created Inner instance. This object is an instance of a non-static member class and is associated with the outer instance. This means that an object of a non-static member class has access to the members within the outer instance. Since the Inner class is nested in the class containing the field secret, this field is accessible to the Inner instance, even though the field secret is declared private.


(b) and (e)

A static member class is in many respects like a top-level class, and can contain non-static fields. Instances of non-static member classes are created in the context of an outer instance. The outer instance is inherently associated with the inner instance. Several inner class instances can be created and associated with the same outer instance. Static classes do not have any inherent outer instance. A static member interface, just like top-level interfaces, cannot contain non-static fields. Nested interfaces are always static.



The code will fail to compile, since the expression ((State) this).val in the method restore() of the class Memento is invalid. The correct way to access the field val in the class State, which is hidden by the field val in the class Memento, is to use the expression State.this.val. Other than that, there are no problems with the code.



The program will compile without error, and will print 1, 3, 4, in that order, when run. The expression B.this.val will access the value 1 stored in the field val of the (outer) B instance associated with the (inner) C object denoted by the reference obj. The expression C.this.val will access the value 3 stored in the field val of the C object denoted by the reference obj. The expression super.val will access the field val from A, the superclass of C.


(c) and (d)

The class Inner is a non-static member class of the Outer class, and its full name is Outer.Inner. The Inner class does not inherit from the Outer class. The method named doIt is, therefore, neither overridden nor overloaded. Within the scope of the Inner class, the doIt() method of the Outer class is hidden by the doIt() method of the Inner class.



Non-static member classes, unlike top-level classes, can have any accessibility modifier. Static member classes can only be declared in top-level or nested static member classes and interfaces. Only static member classes can be declared static. Declaring a class static only means that instances of the class are created without having an outer instance. This has no bearing on whether the members of the class can be static or not.


(d) and (e)

The methods labeled (1) and (3) will not compile, since the non-final parameter i is not accessible from within the inner class. The syntax of the anonymous class in the method labeled (2) is not correct, as the parameter list is missing.


(a) and (d)

No other static members, except final static fields, can be declared within a non-static member class. Members in outer instances are directly accessible using simple names (provided they are not hidden). Fields in nested static member classes need not be final. Anonymous classes cannot have constructors, since they have no names. Nested classes define distinct types from the enclosing class, and the instanceof operator does not take the type of the outer instance into consideration.



Classes can be declared as members of top-level classes. Such a class is a static member class if it is declared static, otherwise, it is a non-static member class. Top-level classes, local classes, and anonymous classes cannot be declared static.