Nested classes and inner classes. Static nested classes. Examples
Contents
- 1. What classes are called nested classes? Features of using nested classes
- 2. Can a nested class exist independently of the enclosing (external) class?
- 3. What are the types of nested classes in terms of access to a class?
- 4. What do the terms “inner class” and “nested class” mean?
- 5. Features of the declaration and use of a static nested class in a non-static class. Example
- 6. Features of using nested classes when both classes are declared as non-static. Example
- 7. Features of using nested classes when both classes are declared as static
- 8. Features of declaring and using a non-static nested class in a static external class
- Related topics
Search other websites:
1. What classes are called nested classes? Features of using nested classes
A nested class is a class that is declared in another class. When declaring a nested class within another (inside the curly brackets { } block), this nested class is perceived as a member of the class enclosing it.
The nested class has access to all data members and methods of the enclosing class (even private data members), since it receives a reference to the external object (the object of the enclosing class) that created it. A class can contain any number of nested classes.
For example
class OutClass { // data members and enclosing class methods // ... class InClass { // data members and methods of nested (inner) class // ... } }
In the above example, a nested class is declared with the name InClass within the scope of the enclosing class named OutClass.
⇑
2. Can a nested class exist independently of the enclosing (external) class?
A nested class cannot exist independently of the enclosing class. However, an object of a nested class can be created.
⇑
3. What are the types of nested classes in terms of access to a class?
There are two types of nested classes:
- static nested classes. Such classes are declared with the static access modifier;
- non-static nested classes or inner classes. These are classes declared within curly brackets of the outer (spanning) class.
⇑
4. What do the terms “inner class” and “nested class” mean?
The inner class is the usual non-static nested class. That is, it is a class that is declared within another class without the static keyword. If a class is declared with the static keyword (static nested class), then this class is also called an nested class.
⇑
5. Features of the declaration and use of a static nested class in a non-static class. Example
If a nested class is declared with the keyword static, the following main points can be identified in this case:
- any public-static class has a global scope, that is, it is visible from all classes. Therefore, you can refer to the name of this class, taking into account the attachment hierarchy. For example, if a static class named B is nested in a class named A, then the call to this class will be A.B;
- from a static nested class, you cannot directly access non-static data members and methods of the enclosing (external) class. However, it is possible to access the static data members of the enclosing class;
- from the enclosing class, it is not possible to have access to non-static public data members and methods of the static inner class directly.
Example.
The OutData class is declared, in which the nested static InData class is declared. The InData class contains
// enclosing class class OutClass { int d; int x; // constructor OutClass() { d = 15; x = 20; } // internal method of class OutClass void DemoOut() { // can declare an object of a nested static class InClass ic = new InClass(); // access to the internal data of the static class InClass through the class object ic.a = 25; ic.d = 33; // it is possible declare an object of the enclosing class OutClass oc = new OutClass(); oc.d = 78; oc.x = 323; } // nested static class static class InClass { int a; int d; // constructor InClass() { // access to variables a, d of this class a = 5; d = 10; // it is not possible to access the d, x variables of the OutClass class, // because there is no access from a static class to a non-static data member //x = 30; - error } // internal method in a static class void DemoIn() { // you can create an object of class OutClass OutClass oc = new OutClass(); oc.d = 55; oc.x = 77; // it is possible to create an object of a static class InClass InClass ic = new InClass(); ic.a = 33; ic.d = 55; } } void DemoMethod() { // access to static class from non-static method OutClass.InClass IC = new OutClass.InClass(); int t; t = IC.a; System.out.println("t = " + t); } }
The following demonstrates the use of the OutClass and InClass classes.
// it is possible to declare objects of both covering and internal classes OutClass.InClass i = new OutClass.InClass(); // create an instance from a static class OutClass o = new OutClass(); // create an instance of a non-static class int t; t = iC.a; // t = 5 t = o.d; // t = 15 // Demonstration of calling methods of classes OutClass and OutClass.InClass iC.DemoIn(); oC.DemoOut();
⇑
6. Features of using nested classes when both classes are declared as non-static. Example
Enclosing and nested classes can be declared without the static keyword. In this case, working with classes has the following features:
- if in a certain class a nested non-static class is declared, then this class is also called the “inner” class;
- from the nested (inner) class there is access to all data members and methods of the enclosing class;
- to use a nested class, an instance of the nested class must be created in the method of the enclosing class.
Example.
class OutClass { int d; int x; // constructor OutClass() { d = 15; x = 20; } // internal method of OutClass class void DemoOut() { // You can declare an object of a nested class from a external class. InClass ic = new InClass(); ic.a = 25; ic.d = 33; // can declare an object of the external class OutClass oc = new OutClass(); oc.d = 78; oc.x = 323; // You cannot directly access data and methods of a nested class from the external class, // since no nested class instance was created // InClass.this.a = 22; - error! } // nested non-static class class InClass { int a; int d; // constructor InClass() { // access to variables a, d of the nested class InClass a = 5; // OutClass.InClass.this.a = 5 OutClass.InClass.this.d = 10; // d = 10; // you can access the variables d, x of class OutClass, x = 30; OutClass.this.d = 35; } // internal method void DemoIn() { // you can create an object of class OutClass OutClass oc = new OutClass(); oc.d = 55; oc.x = 77; // you can create an object of class InClass InClass ic = new InClass(); ic.a = 33; ic.d = 55; } } }
⇑
7. Features of using nested classes when both classes are declared as static
Static classes have a global scope from all other classes and methods (static and non-static) if they are declared public.
Features of using nested static classes:
- it is possible to declare instances of nested public-static classes;
- it is prohibited to declare a static class at the access level of a package (see Figure 1-1);
- a static class can be declared as a nested class in another non-static class as demonstrated in Section 6 of this topic (Figure 1-2).
Figure 1. The declaration of a static nested class: 1 – an error, it is forbidden to announce within the package; 2 – allowed to declare within class
A static nested class can be declared in a static spanning class. However, this static enclosing class can be implemented inside a non-static enclosing class. In this case, there are two nested levels.
The following is a simplified example of declaring a static nested class in a static enclosing class.
// enclosing non-static class class OutClass { int d; // nested static class static class InClass { int d; // nested static class in the static covering class InClass static class InClass2 { int d2; } } }
As can be seen from the above code, the class OutClass is declared, which is external with respect to the static class InClass. In turn, the static class InClass is external in relation to the static class InClass2. Using an object of class InClass2 may be as follows
// declare the object of class OutClass.InClass.InClass2 OutClass.InClass.InClass2 obj = new OutClass.InClass.InClass2(); int t; obj.d2 = 23; // OutClass.InClass.InClass2.d t = obj.d2;
⇑
8. Features of declaring and using a non-static nested class in a static external class
In a static external class, it is possible to declare a non-static nested class. However, when trying to create an instance of such a non-static class, the Java compiler will generate an error
No enclosing instance of type OutClass.InClass is accessible. Must qualify the allocation with an enclosing instance of type OutClass.InClass (e.g. x.new A() where x is an instance of OutClass.InClass).
So, the use of non-static nested classes in static covering classes is prohibited.
⇑
Related topics
- Creating the objects of nested static and non-static classes. Constructions .this, .new
- The use of classes in Java programs. Class and object definition. Examples