Types of Nested classes
- Member Inner Class ( Non-static Nested Class ).
- Static Nested Class.
- Local (nested , inner) Class
- Anonymous (nested , inner) Class.
- If you declare a nested class as static it will called Nested Static Class.
- Non static nested class are simply referred as Inner Class.
- For outer class, there is only two access modifier for it, public and package.
- Inside an interface, the nested class or interface implicitly public and static.
Member Inner Class
- Inner classes cannot have static declarations unless the member is a constant variable.
- An inner class may inherit static members that are not constant variables even
though it cannot declare them. - Access fields of the enclosing class even if they are declared private.
- Java makes it possible though, for the Inner class to refer to the text field of the Outer class. To do so it has to prefix the text field reference with Outer.this. (the outer class name + .this. + field name).
- If a Java inner class declares fields or methods with the same names as field or methods in its enclosing class, the inner fields or methods are said to shadow over the outer fields or methods.
public class Outer {
public class Inner {
}
}
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
Nested Static Class
- One of the main benefits of nested static class over inner class is that
instance of nested static class is not attached to any enclosing
instance of Outer class.
(You don't need any instance of Outer class to create instance of nested static class in Java) - Non-static variable cannot be referenced from a static context.
- They are typically declared as either subclasses of an existing class, or as implementations of some interface.
public class Outer {
public static class NestedStaticClass {
}
}
Outer.NestedStaticClass obj = new Outer.NestedStaticClass();
Local Class
- You can define a local class inside any block ( method body, for loop, if clause ).
- A local class can access local variables only that are declared final.(local variable is accessed from within inner class; needs to be declared final)
From Java 8 local classes can also access local variables and parameters of the method the local class is declared in. The parameter will have to be declared final or be effectually final.
Effectually final means that the variable is never changed after it is initialized. - You cannot declare static method in a local class.
Since you cannot declare a local variable static, you also
cannot declare a local class static.
class Outer {
public void printText() {
class Local {
}
Local local = new Local();
}
}
Anonymous Class
- You cannot declare constructors in an anonymous class.
- Anonymous classes are ideal if you have to implement an interface that contains two or more methods.
Can used in performing additional tasks such as method overloading - Anonymous classes are defined in the new expression itself, so you cannot create multiple objects of an anonymous class.
- An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.
- There are no such things as anonymous interfaces.
- You cannot explicitly extend a class or explicitly implement interfaces when defining an
anonymous class.
public class SuperClass {
public void doIt() {
System.out.println("SuperClass doIt()");
}
}
SuperClass instance = new SuperClass() {
public void doIt() {
System.out.println("Anonymous class doIt()");
}
};
instance.doIt();
Benefits of Nested ClassesThe organizational advantage.
The callback advantage.
Implement helper classes.
Implement same interface multiple times inside class.
http://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html
http://tutorials.jenkov.com/java/nested-classes.html
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
www.javaworld.com/article/2077411/core-java/inner-classes.html
No comments:
Post a Comment