Ποιοι τύποι εσωτερικών κλάσεων υπάρχουν στη γλώσσα προγραμματισμού;
sobes.tech AI
Απάντηση από AI
Nested classes in Java:
-
Static Nested Classes: Behave like regular static members of the class. They do not have access to non-static members of the outer class. 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: Associated with an instance of the outer class. Have full access to all (static and non-static) members of the outer class, including private ones. 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). Only available within that block. Can refer to
finalor effectivelyfinallocal variables and parameters, 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: Classes without a name. Used for declaring and creating an instance of a class that implements an interface or extends a class, simultaneously. Useful 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 | Is an instance of the outer class needed for creation | Access modifiers |
|---|---|---|---|---|
| Static nested class | Inside the class, with the static keyword. |
Only to static members. | No | All |
| Inner class | Inside the class (non-static). | To all (static and non-static) members. | Yes | All |
| Local class | Inside a block (methods, constructors, initializers). | To all outer class members + final or effectively final local variables and parameters. |
Yes | No (final or abstract implicitly) |
| Anonymous class | Inside a block (expression). | To all outer class members + final or effectively final local variables and parameters. |
Yes | No |