Menu

Nested types

Will Pittenger

Modern object oriented programming requires nested types. You've already seen some nested types in previous sections of this wiki. This section concentrates on how nested classes interact with their enclosing class(es). In ASIL, the only types you can declare that's not allowed to declare a nested type are primitive-based enums.

Until ASIL, nested classes came in two varieties. C++ and C# have simple nested classes where the nested class could access any static member of the enclosing type, but lack a reference/pointer to an instance of that enclosing type. Java nested types (called inner types) were different. If you just declared a nested type, the nested class can exist only with in the context of the enclosing class. All its members, static and non-static, are available. Java changes that if you declare the nested type with the static keyword. Now its access to the enclosing class is that of a non-nested type. You can only access static members of the enclosing class with a fully qualified identifier.

ASIL merges these two types of nested classes. If you just declare an nested type, ASIL gives you a type similar to that used by C++ and C#. You have access to all static members/methods of the enclosing type, event private members/methods. The one thing you can't do is create a new instance of the enclosing type using any private constructors it declares (at least directly).

On the other hand, if you declare a class with the inner qualifier keyword, you get a class more like Java's static inner class system. (Don't confuse a Java inner class with an ASIL inner class.) Please note that static classes can't contain inner classes. Nor can you use both static and inner on the same type. Finally, the inner qualifier keyword can't be applied to primitive-based enums or interfaces.

Unlike Java non-static inner classes, ASIL inner classes can have static members. The authors of Java decided that static members inside non-static inner classes didn't make sense. To them, those members should below to the enclosing class. However, it makes sense to allow static members inside ASIL inner classes for scope reasons.

class OuterClass
  class NestedClass1
    ' Add some members; This class can only reference static members and types declared in OuterClass

  class inner NestedClass2
    ' Add some members; This class an only exist in the context of an instance of OuterClass, but can reference all members of OuterClass, even non-static members

Related

Wiki: Home