NETJVM 

Nested Classes in Managed COBOL

In managed COBOL, you can define a nested class within another class, following all the methods belonging to the containing class.

Nested classes can access static fields, properties and methods within the containing classes.

The optional SHARING PARENT phrase in the nested class definition enables instance methods and properties in the nested class to access the instance fields, properties and methods in the containing class.

    class-id ContainingClass. 
    ...
    class-id Nested1. 
        ... 
    end class.
    class-id Nested2 sharing parent. 
       ... 
    end class. 
    end class.

Nested Classes with SHARING PARENT

Nested classes with the SHARING PARENT phrase may only be instantiated from within an instance method in the enclosing class. They can access the instance members from that containing class, even when those instance members are private.

Such a nested class contains an implicit reference to an instance of its containing class. The nested class uses this reference to access the enclosing class's instance members. This means that the containing class instance will not be garbage collected until all references to the inner class held externally have themselves been garbage collected.

For example:

    class-id ContainingClass. 
    01 companyName string value "Micro Focus".
    
    method-id instance1.
    01 o type Nested2.
        set o to new Nested2
        ...
    end method

    class-id Nested2 SHARING PARENT. 
    method-id method2
        display companyName
    end method.
    end class.

    end class.

Nested Classes without SHARING PARENT

Nested classes defined without the SHARING PARENT phrase may be declared as public, private or internal. Depending on the visibility specified, they can be instantiated from any context, not just from the containing class, and not just from instance methods. They can only access instance members of the containing class using an explicit object reference.