Nested classes, local, anonymous in Java
What are the peculiarities of using nested classes: static and internal. What is the difference between them
- Nested classes can access all members of the enclosing class, including private ones.
- You do not need an outer class object to create a static nested class object.
- From an object of a static nested class, you cannot access non-static members of the enclosing class directly, but only through a reference to an instance of the outer class.
- Regular nested classes cannot contain static methods, initialization blocks, and classes. Static nested classes can.
- An object of an ordinary nested class stores a reference to an object of the outer class. There is no such link inside a static link. An instance of the enclosing class is accessed by specifying .this after its name. For example: Outer.this.
"local class"
Local inner class is a nested class that can be declared in any block in which variables are allowed to be declared. Like simple Member inner classes, local classes are named and reusable. Like anonymous classes, they have an instance around them only when they are used in a non-static context.
Local classes have the following features:
- Visible only within the block in which they are declared;
- Cannot be declared as private/public/protected or static;
- They cannot have static declarations of methods and classes inside themselves, but they can have final static fields, initialized with a constant;
- Have access to the fields and methods of the enclosing class;
- They can access local variables and method parameters if they are declared final.
"anonymous class"
It is a nested local class with no name and is allowed to be declared anywhere in the enclosing class that allows expression placement. The creation of an instance of an anonymous class occurs simultaneously with its declaration. Depending on the location, the anonymous class behaves like a static or non-static nested class - the surrounding instance appears in the non-static context.
Anonymous classes have several limitations:
- Their use is allowed only in one place of the program - the place of its creation;
- Application is possible only if after the instance is spawned there is no need to refer to it;
- It implements only methods of its interface or superclass, i.e. cannot declare any new methods, since there is no named type to access them.
Anonymous classes are commonly used for:
- creating a function object, for example, implementing the Comparator interface;
- creating a process object, such as instances of Thread, Runnable, and the like;
- in a static generation method;
- initialization of a public static final field, which corresponds to a complex enumeration of types where a separate subclass is required for each instance in the enumeration.
How to access the field of the outer class from the nested class
A static nested class has direct access only to the static fields of the enclosing class.
A simple inner class, can access any field of the outer class directly. If a nested class already has a field with the same literal, then you should refer to such a field through a link to its instance. For example: Outer.this.field.
Read also:
- Types of classes in Java. Nested classes
- Overriding and overloading methods in Java
- What is the order of calling constructors and initialization blocks, given the class hierarchy
Comments
Post a Comment