What types of nested classes exist in programming languages?
sobes.tech AI
Answer from AI
Nested classes in Java:
-
Static Nested Classes: Behave like static members of the class. They do not have access to non-static members of the outer class. They are created without an instance of the outer class.
public class Outer { private static String staticMsg = "Static message"; private String instanceMsg = "Instance message"; static class StaticNested { void displayStatic() { // Access to static members of the outer class System.out.println(staticMsg); // No access to non-static members of the outer class // System.out.println(instanceMsg); // Compilation error } } } -
Inner Classes: Are associated with an instance of the outer class. They have full access to all (static and non-static) members of the outer class, including private ones. They can be declared
private,protected, package-private, orpublic.public class Outer { private static String staticMsg = "Static message"; private String instanceMsg = "Instance message"; class Inner { void display() { // Access to static and non-static members of the outer class System.out.println(staticMsg); System.out.println(instanceMsg); } } } -
Local Classes: Declared within a block (method, constructor, or static initializer). They are only accessible within that block. They can access
finalor effectivelyfinallocal variables and parameters of the block, as well as all members of the outer class.public class Outer { private String instanceMsg = "Instance message"; void methodWithLocalClass() { final String localVar = "Local variable"; // or effectively final class Local { void display() { System.out.println(instanceMsg); System.out.println(localVar); } } Local localObj = new Local(); localObj.display(); } } -
Anonymous Classes: Are unnamed local classes. They are used for simultaneous declaration and instantiation of a class that either implements an interface or extends a class. They are convenient for creating small, one-time objects.
public class Outer { void methodWithAnonymousClass() { // Anonymous class implementing Runnable Runnable myRunnable = new Runnable() { @Override public void run() { System.out.println("Running from anonymous class"); } }; new Thread(myRunnable).start(); // Anonymous class extending Thread Thread myThread = new Thread() { @Override public void run() { System.out.println("Running from anonymous class extending Thread"); } }; myThread.start(); } }
Comparison of nested class types:
| Class Type | Declaration Location | Access to Outer Class Members | Requires Outer Class Instance for Creation | Access Modifiers |
|---|---|---|---|---|
| Static Nested Class | Inside class with static keyword. |
Only static members | No | All |
| Inner Class | Inside class (non-static). | All (static and non-static) members | Yes | All |
| Local Class | Inside a block (methods, constructors, initializers). | All outer class members + final/effectively final local variables |
Yes | None (final or abstract implicitly) |
| Anonymous Class | Inside a block (expression). | All outer class members + final/effectively final local variables |
Yes | No |