Creating the objects of nested static and non-static classes. Constructions .this, .new

Creating the objects of nested static and non-static classes. Constructions .this, .new


Contents



1. Is it possible to declare a nested class object in an enclosing class?

Yes, it is. Below is an example of declaring an object named ic of the nested class InClass in the body of the enclosing class OutClass.

// enclosing non-static class
class OutClass {
    int d;
    InClass ic = new InClass(); // the object of nested class

    // nested non-static class
    class InClass {
        int d1;
    }
}

Creating and using an instance of the class OutClass can be, for example, the following.

OutClass obj = new OutClass();
obj.ic.d1 = 23;

 

2. Features of creating objects in the case of declaring a nested static class

If a nested static class is declared in a non-static class, then:

  • in order to create an object of a nested static class, it is not necessary to create an object of the enclosing class. The access to the name of the nested static class named InClass is done according to the pattern OutClass.InClass, where OutClass is the name of the enclosing class;
  • you cannot access members of the enclosing class from a static nested class.

 

3. How to get a reference to an object of an external (covering) class? The .this construction

To get a reference to an object of the outer class, use the construction

OutClass.this

here OutClass – the name of the external (covering) class. The .this construction is only suitable for nested non-static classes.

 

4. An example of receiving a reference to an external class object

The example declares an enclosing class OutClass. In this class, a non-static class InClass is declared, containing the method ReturnObjOutClass().This method returns a reference to an object of the enclosing class OutClass using the .this construction.

// enclosing non-static class
class OutClass {
    int d;
    InClass ic = new InClass(); // the object of nested class

    // nested non-static class
    class InClass {
        int d1;

        // the method returns a reference to the object of the outer class OutClass
        OutClass ReturnObjOutClass() {
            // return a reference to an object of class OutClass
            return OutClass.this;
        }
    }
}

The following demonstrates the use of the OutClass class and the ReturnObjOutClass() method in another method.

// demonstration of the use of the .this pointer to an outer class object
OutClass obj = new OutClass(); // create an instance (object) of class OutClass
int t;

// fill in the values of the internal data of the object
obj.d = 76;
obj.ic.d1 = 23;

// declare a new reference like OutClass
OutClass obj2;

// invoke the method that returns a reference to an external class object
obj2 = obj.ic.ReturnObjOutClass(); // now obj2 and obj refer to the same object

// checking
t = obj2.ic.d1; // t = 23, conclusion: obj2 and obj refer to the same object
t = obj2.d; // t = 76

 

5. How to create an object of a nested class in case of non-static nested classes? The ‘.new’ construction

To create an object of a nested class, you need to use a following construct

ObjOutClass.new

here ObjOutClass – the name of the object of enclosing class.

If a nested class is declared as non-static (inner class), then the following rule applies:

  • in order to create an object of a nested class, an object of the enclosing class must be created. It will not be possible to create an object of the nested class without an object of the enclosing class. This is because the object of the nested class is imperceptibly associated with the object of the outer class.

 

6. An example of creating an inner class object using the .new construct

The following shows how to create an inner class object in the main() function. The class OutClass is defined, in which the inner class InClass is declared.

package Package1;

// enclosing non-static class
class OutClass {
    int d;

    // nested class object created in a standard way
    InClass ic = new InClass();

    // nested non-static class
    class InClass {
        int dd;
    }
}

public class NestedClasses {
    public static void main(String[] args) {
        // Demonstration of the use of the .new construct on a nested class object
        OutClass oc = new OutClass(); // create an object of class OutClass
        int t;

        // invoke a .new construct to create an object of a nested class
        OutClass.InClass ic = oc.new InClass();

        // using an object of a nested class
        ic.dd = 37;
        t = ic.dd; // t = 37
        System.out.println("t = " + t);
    }
}

 

7. Is it possible to create an object of a nested static class?

Yes, it is. In this case, in an external class, a static nested class must be declared as public. If a static class is declared as private, then an object of this class cannot be created.

 

8. How to create an object of a nested static class?

If the nested class is declared as static, then you can declare the object of the nested static class in standard way

OutClass.InClass obj = new OutClass.InClass();

because the static nested OutClass.InClass class has a global scope.

 

9. Example that demonstrates the creation of objects of nested static classes

An OutClass class is defined in which two nested static classes are declared with the names InClass1, InClass2. The class InClass1 by default has a public access type. The second class InClass2 is declared as private.

// external non-static class
class OutClass {
    int d;

    // nested static public-class
    static class InClass1 {
        int d1;
    }

    // nested static private-class
    private static class InClass2 {
        int d2;
    }
}

The following demonstrates the creation and use of objects of classes OutClass, InClass1 from other methods that are located in other classes.

public class MyStaticClass {
    public static void main(String[] args) {
        // Demonstration of the use of the .new construct on a nested class object
        OutClass oc = new OutClass(); // create an object of class OutClass
        int t;

        // access to data of class OutClass through object oc
        oc.d = 233;

        // create an object of a nested static public class OutClass.InClass1()
        OutClass.InClass1 ic1 = new OutClass.InClass1();

        // access to the data of the class OutClass.InClass1() using the object
        ic1.d1 = 777;

        // create an object of a nested static private class - prohibited
        // OutClass.InClass2 ic2 = new OutClass.InClass2(); - error!
        System.out.println("t = " + t);
    }
}

As you can see from the above code, you can declare objects of the OutClass and InClass1 classes from other methods. However, there is no access to the hidden static class InClass2. In this case, the static keyword does not help.

 


Related topics