Inner classes are a security mechanism in Java. We know a class cannot be associated with the access modifier private, but if we have the class as a member of other class, then the inner class can be made private. Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private. Mainly three types of Inner Classes are
- Nested inner class
- Method local inner class
- Static inner class
- Anonymous inner class
Important points about inner class
- We need class object to access the Local Inner Class in which it exist.
- Static Inner Class get directly accessed same as like any other static method of the same class in which it is exists.
- Anonymous Inner Class are not visible to out side world as well as to the other methods or classes of the same class(in which it is exist) and it is used on the point where it is declared.
Nested Inner class
A nested inner class has access to the members of its enclosing class. It can access local variables and parameters of the enclosing block that are final or effectively final. A variable or parameter whose value is never changed after it is initialized is effectively final.
// Base class public static class BaseClass { public static void validateLocalClass(String phNum1, String phNum2) { // Nested inner class class LocalClass { String formattedLocalClass = null; LocalClass(String LocalClass){ formattedLocalClass = phNum2; // Not allowed to modify, Enclosing block (BaseClass) // variables are final or effectively final. phNum1 = "ph1"; } } } public static void main(String... args) { validateLocalClass("123-456-7890", "456-7890"); } }
Method Local inner classes
Inner class can be declared within a method of an outer class.
class BaseClass { void outerMethod() { // Inner class is local to this method class Inner { void innerMethod() { System.out.println("inside innerMethod"); } } Inner y = new Inner(); y.innerMethod(); } } class MethodDemo { public static void main(String[] args) { BaseClass x = new BaseClass(); x.outerMethod(); } }
Static inner class
If the nested class is static, then it’s called a static nested class. Static nested classes can access only static members of the outer class. A static nested class can be accessed, as with other static members, without having an instance of the outer class.
public class InnerClass { // Static inner class static class StaticInnerClass { public void staticInnerClassMethod() { System.out.println("Static Inner class!"); } } // Local inner class class LocalInnerClass { public void localInnerClassMethod() { System.out.println("Local Inner class!"); } } public static void main(String args[]) throws InterruptedException { // Direct access to inner class method new InnerClass.StaticInnerClass().staticInnerClassMethod(); // Static inner class reference object StaticInnerClass staticInnerclass = new StaticInnerClass(); staticInnerclass.staticInnerClassMethod(); // Access local inner class LocalInnerClass localInnerClass = new InnerClass().new LocalInnerClass(); localInnerClass.localInnerClassMethod(); // Anonymous class AnonymousClass anonymousClass = new AnonymousClass() { // your code goes here... }; } }
Anonymous Classes
An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator. While a local class definition is a statement in a block of Java code, an anonymous class definition is an expression, which means that it can be included as part of a larger expression, such as a method call. When a local class is used only once, consider using anonymous class syntax, which places the definition and use of the class in exactly the same place.
Example:-
// Override compare function Comparator<String> localeComparator = new Comparator<String>() { @Override public int compare(String arg0, String arg1) { return arg0.compareTo(arg1); } };