{"id":2136,"date":"2020-03-16T17:44:16","date_gmt":"2020-03-16T17:44:16","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=2136"},"modified":"2024-12-24T09:32:09","modified_gmt":"2024-12-24T14:32:09","slug":"nested-classes-in-java","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/nested-classes-in-java\/","title":{"rendered":"Nested Classes in Java"},"content":{"rendered":"\n<p><span style=\"font-weight: 400;\">Nested classes in Java are classes that are defined within the scope of another class. They are used to logically group classes that are only used in one place, and they can help increase encapsulation and readability. In Java, there are four types of nested classes:<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\"> Here is the simplest example:<\/span><\/p>\n\n\n\n<p><i><span style=\"font-weight: 400;\">public class Oyster {<br><\/span><\/i><i><span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public class Pearl {<br><\/span><\/i><i><span style=\"font-weight: 400;\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br><\/span><\/i><i><span style=\"font-weight: 400;\">}<\/span><\/i><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">There are various types of nested classes, and in this article, we are going to review them in detail.&nbsp;<\/span><\/p>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p><b>Static nested classes&nbsp;<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Similar to static fields and methods, a static nested class belongs to the enclosing class and not to an instance of the class. Also, it does not have access to the instance variables and the methods of the outer class. But it can access static variables and methods of the outer class.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The static nested class can be declared with all types of access modifiers.<br><\/span><span style=\"font-weight: 400;\">A static <a href=\"https:\/\/www.h2kinfosys.com\/courses\/java-online-training-course-details\">nested java class<\/a> is nested for only packaging convenience.&nbsp;<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">Here is an example:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public\u00a0class\u00a0Outer {\n\n\u00a0 \u00a0\u00a0private\u00a0int\u00a0a\u00a0= 1;\n\n\u00a0 \u00a0\u00a0protected\u00a0static\u00a0int\u00a0b\u00a0= 1;\n\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0int\u00a0c\u00a0= 1;\n\n\u00a0 \u00a0\u00a0static\u00a0int\u00a0d\u00a0= 1;\n\n\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0class\u00a0StaticNested {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0test1() {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ System.out.println(\"Outer class field \" + a); \/\/ NOT COMPLILED\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0new\u00a0Outer().a);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0b);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0c);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0d);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0public\u00a0void\u00a0test2() {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Non-static method\");\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0\u00a0}\n\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">As you can see, the outer class field without modifier static cannot be called inside the nested static class. But after the creation of an instance of Outer class, we can use any variable of Outer class, even with private modified. In this example also shown that the nested static class can access all static fields of the outer class.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Let&#8217;s try to call a method from our static nested class:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public\u00a0static\u00a0void\u00a0main(String&#91;]\u00a0args) {\n\n\u00a0 \u00a0\u00a0\/\/call of static method of static nested class\n\n\u00a0 \u00a0\u00a0Outer.StaticNested.test1();\n\n\u00a0 \u00a0\u00a0\/\/call of non-static method of static nested class\n\n\u00a0 \u00a0\u00a0new\u00a0Outer.StaticNested().test2();\n\n}<\/code><\/pre>\n\n\n\n<p><b>Non-static nested classes&nbsp;<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Non-static nested classes are also called inner.&nbsp; They can have any access modifier. The inner class belongs to an instance of the enclosing class, the same as its other members(fields and methods). Non-static nested classes have access to all static and non-static members of the enclosing class.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Let&#8217;s look at the example of an inner class:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public\u00a0class\u00a0Outer {\n\n\u00a0 \u00a0\u00a0private\u00a0int\u00a0a\u00a0= 1;\n\n\u00a0 \u00a0\u00a0protected\u00a0int\u00a0b\u00a0= 1;\n\n\u00a0 \u00a0\u00a0int\u00a0c\u00a0= 1;\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0int\u00a0d\u00a0= 1;\n\n\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0public\u00a0class\u00a0Inner {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0private\u00a0int\u00a0innerField\u00a0= 1;\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ private static\u00a0int\u00a0staticInnerField = 1;\/\/ NOT COMPLILED\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0public\u00a0void\u00a0test1() {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0a);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0b);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0c);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0d);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Inner class field \"\u00a0+\u00a0innerField);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ public static void test2() {} \/\/ NOT COMPLILED\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0}\n\n}\n\nYou can see that the Inner class can call any member with any modifier of the Outer class.\u00a0 An inner class cannot have any static field or method, but all access modifiers are applicable to Inner class members. Here is how we can call the inner class:\n\npublic\u00a0static\u00a0void\u00a0main(String&#91;]\u00a0args) {\n\n\u00a0 \u00a0\u00a0\/\/ instantiating outer class\n\n\u00a0 \u00a0\u00a0Outer\u00a0outer\u00a0=\u00a0new\u00a0Outer();\n\n\u00a0 \u00a0\u00a0\/\/ instantiating inner class\n\n\u00a0 \u00a0\u00a0Outer.Inner\u00a0inner\u00a0=\u00a0outer.new\u00a0Inner();\n\n\u00a0 \u00a0\u00a0inner.test1();\n\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">As you can see, to create an inner class, we must first create its enclosing class.&nbsp;<\/span><\/p>\n\n\n\n<p><b>Local Inner classes<br><\/b><span style=\"font-weight: 400;\">The local inner classes are the special type of inner classes, in which the class is defined inside a method or scope block. Let&#8217;s look at different places of&nbsp; local inner classes declarations:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public\u00a0class\u00a0Outer {\n\n\u00a0 \u00a0\u00a0private\u00a0int\u00a0a\u00a0= 1;\n\n\u00a0 \u00a0\u00a0protected\u00a0int\u00a0b\u00a0= 1;\n\n\u00a0 \u00a0\u00a0int\u00a0c\u00a0= 1;\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0int\u00a0d\u00a0= 1;\n\n\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0public\u00a0class\u00a0Inner {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0private\u00a0int\u00a0innerField\u00a0= 1;\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ private static\u00a0int\u00a0staticInnerField = 1;\/\/ NOT COMPLILED\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0public\u00a0void\u00a0test1() {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0a);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0b);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0c);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Outer class field \"\u00a0+\u00a0d);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Inner class field \"\u00a0+\u00a0innerField);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ public static void test2() {} \/\/ NOT COMPLILED\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0}\n\n}\n\nYou can see that the Inner class can call any member with any modifier of the Outer class.\u00a0 An inner class cannot have any static field or method, but all access modifiers are applicable to Inner class members. Here is how we can call the inner class:\n\npublic\u00a0static\u00a0void\u00a0main(String&#91;]\u00a0args) {\n\n\u00a0 \u00a0\u00a0\/\/ instantiating outer class\n\n\u00a0 \u00a0\u00a0Outer\u00a0outer\u00a0=\u00a0new\u00a0Outer();\n\n\u00a0 \u00a0\u00a0\/\/ instantiating inner class\n\n\u00a0 \u00a0\u00a0Outer.Inner\u00a0inner\u00a0=\u00a0outer.new\u00a0Inner();\n\n\u00a0 \u00a0\u00a0inner.test1();\n\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">All of the above declarations are legal.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">The method inner classes are not associated with an Object. Therefore, they cannot have any access modifiers in their declaration. The only allowed modifiers for local inner classes are abstract or final. The local inner classes have access to static and non-static members in the enclosing context. You can read the local variable\u2019s values. But if you try to modify a local variable in method inner class, you will get a compile-time error. Let&#8217;s look at one more example:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public\u00a0class\u00a0Outer {\n\n\u00a0 \u00a0\u00a0private\u00a0String\u00a0outerVariable\u00a0=\u00a0\"outer\";\n\n\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0public\u00a0void\u00a0test() {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0String\u00a0localVariable\u00a0=\u00a0\"local\";\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0class\u00a0Inner3{\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0public\u00a0void\u00a0test()\u00a0{\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(outerVariable);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(localVariable);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0outerVariable\u00a0=\u00a0\"test\";\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ localVariable = \"test\"; \/\/NOT COMPILED\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ We are getting the compile time error:Local\u00a0\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ variable localVariable defined in an enclosing\u00a0\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/scope must be final or effectively final\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0\u00a0}\u00a0 \u00a0\u00a0\n\n}<\/code><\/pre>\n\n\n\n<p>Let&#8217;s look at the example with an instantiation of a local inner class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public\u00a0class\u00a0Outer {\n\n\u00a0 \u00a0\u00a0static\u00a0{\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0class\u00a0Inner1 {}\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0Inner1\u00a0inner\u00a0=\u00a0new\u00a0Inner1();\n\n\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0public\u00a0void\u00a0test1() {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ Inner1 inner = new Inner1(); \/\/NOT COMPILED,\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ because we are out of scope\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0class\u00a0Inner2{\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0public\u00a0void\u00a0test() {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Hello from the local inner class!\");\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0Inner2\u00a0inner\u00a0=\u00a0new\u00a0Inner2();\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0inner.test();\n\n\u00a0 \u00a0\u00a0}\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0public\u00a0void\u00a0test2() {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ Inner2 inner = new Inner2();\/\/NOT COMPILED,\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ because we are out of scope\n\n\u00a0 \u00a0\u00a0}\n\n}<\/code><\/pre>\n\n\n\n<p><b>Anonymous classes<br><\/b><span style=\"font-weight: 400;\">In Java language, it is possible to create an inner class without a name, such kind of classes are called anonymous. For anonymous classes, definition and instantiation are combined in one single statement.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">An anonymous class has to implement an interface or to extend a class.&nbsp; It is not possible to define a constructor for an anonymous class, because it does not have any name. Because of the same reason, we can access anonymous inner classes only at the point where they are defined.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Let&#8217;s look at the example. First of all, we need to have a class or an interface:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>abstract\u00a0class\u00a0AbstractClass {\n\n\u00a0 \u00a0\u00a0abstract\u00a0void\u00a0doIt();\n\n}<\/code><\/pre>\n\n\n\n<p>Here you can see our anonymous inner class declaration, instantiation, and call of its method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public\u00a0class\u00a0Test {\n\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String&#91;]\u00a0args) {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0String\u00a0localVariable\u00a0=\u00a0\"local variable\";\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/anonymous inner class declaration and instantiation\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0AbstractClass\u00a0anonymousClass\u00a0=\u00a0new\u00a0AbstractClass() {\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0void\u00a0doIt() {\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"Hello from anonymous inner class!\");\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(\"We can use here \"+localVariable);\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0};\n\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0anonymousClass.doIt();\n\n\u00a0 \u00a0\u00a0}\n\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">The specific of the anonymous inner classes is that they can implement only one interface or extend only one class at one time. We can group anonymous inner classes into 3 types based on declaration and behavior: an anonymous class that extends a class,&nbsp; an anonymous class that implements an interface, an anonymous class that defines inside method or constructor argument.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">As for other inner classes in anonymous inner classes, you can read local variables, but you cannot modify them.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">There are a lot of possible use cases of anonymous classes. For example, they are widely used for UI Event Listeners. Here is a code snippet where we add a listener to a button:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button.addActionListener(new ActionListener() {\n\n\u00a0 \u00a0 public void actionPerformed(ActionEvent e) {\n\n\u00a0 \u00a0 \u00a0 \u00a0 ...\n\n\u00a0 \u00a0 }\n\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">In this example, the instance of the anonymous class that implements interface ActionListener is created here. Now, when you click on the button, the <\/span><i><span style=\"font-weight: 400;\">actionPerformed <\/span><\/i><span style=\"font-weight: 400;\">method will be triggered.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Anonymous classes give us the possibility to create a better class hierarchy and to reach a better encapsulation. We usually use anonymous classes in case of modifying the implementation of methods of some classes on the fly. It helps us to avoid adding redundant new *.java files to the project. This is especially good if the class from that added file would be used just one time. Therefore we will get a cleaner project structure. Since Java 8, we can replace some anonymous inner classes with lambda expressions. But we can use lambda expressions just in case of functional interfaces(interface with single abstract method).&nbsp; They implement only abstract functions and implement functional interfaces.&nbsp;&nbsp;<\/span><\/p>\n\n\n\n<p><b>Shadowing<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">Fields and methods from the enclosing class can be shadowed with fields and methods from the inner class. It can be when inner class members have the same names.<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">In this situation, if we want to use members of an outer class, we have to use its name. Here is an example:<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public\u00a0class\u00a0Outer {\n\n\u00a0 \u00a0\u00a0private\u00a0String\u00a0string1\u00a0=\u00a0\"outer1\";\n\n\u00a0 \u00a0\u00a0private\u00a0static\u00a0String\u00a0string2\u00a0=\u00a0\"outer2\";\n\n\u00a0 \u00a0\u00a0public\u00a0class\u00a0Inner\u00a0{\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0String\u00a0string1\u00a0=\u00a0\"inner1\";\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0final\u00a0static\u00a0String\u00a0string2\u00a0=\u00a0\"inner2\";\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0public\u00a0void\u00a0test() {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ printing values from Inner class\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(string1);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(string2);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\/\/ printing values from Outer class\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(Outer.this.string1);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(Outer.string2);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0System.out.println(Outer.this.string2);\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0}\n\n\u00a0 \u00a0\u00a0}\n\n}\n\nLet's call our\u00a0test()\u00a0method and look at the result:\n\npublic\u00a0class\u00a0Test {\n\n\u00a0 \u00a0\u00a0public\u00a0static\u00a0void\u00a0main(String&#91;]\u00a0args) {\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0Outer\u00a0outer\u00a0=\u00a0new\u00a0Outer();\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0Outer.Inner\u00a0inner\u00a0=\u00a0outer.new\u00a0Inner();\n\n\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0inner.test();\n\n\u00a0 \u00a0\u00a0}\n\n}<\/code><\/pre>\n\n\n\n<p><span style=\"font-weight: 400;\">After running this application we will get printed this:<\/span><\/p>\n\n\n\n<p><i><span style=\"font-weight: 400;\">inner1<br><\/span><\/i><i><span style=\"font-weight: 400;\">inner2<br><\/span><\/i><i><span style=\"font-weight: 400;\">outer1<br><\/span><\/i><i><span style=\"font-weight: 400;\">outer2<br><\/span><\/i><i><span style=\"font-weight: 400;\">outer2<\/span><\/i><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">As you can understand, we were able to call successfully outer class and inner class values, even their names were overlapped.<\/span><\/p>\n\n\n\n<p><b>Serialization of&nbsp; Nested classes<\/b><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">There are some nuances with inner classes related to serialization. We can get&nbsp;<\/span><\/p>\n\n\n\n<p><span style=\"font-weight: 400;\">a java.io.NotSerializableException in case of ignoring these rules:<\/span><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span style=\"font-weight: 400;\">you must declare the nested class as static.<\/span><\/li>\n\n\n\n<li><span style=\"font-weight: 400;\">you have to make both the nested class and the enclosing class implement Serializable.<\/span><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion for nested classes<\/h3>\n\n\n\n<p>Nested classes in Java are a powerful feature that allow developers to logically group classes, encapsulate functionality, and improve code organization. However, they should be used judiciously to avoid unnecessary complexity. By understanding the different types of nested classes and their appropriate use cases, developers can write more maintainable and efficient Java code.<\/p>\n\n\n\n<p>In addition, nested classes often provide a way to keep related functionality together, making code easier to navigate and understand. For example, static nested classes are ideal for utility-like components that don&#8217;t depend on instance members of the enclosing class, while inner classes are beneficial when they need direct access to the enclosing class&#8217;s data. By leveraging nested classes appropriately, you can enhance <a href=\"https:\/\/www.geeksforgeeks.org\/encapsulation-in-java\/\" data-type=\"link\" data-id=\"https:\/\/www.geeksforgeeks.org\/encapsulation-in-java\/\" rel=\"nofollow noopener\" target=\"_blank\">encapsulation <\/a>and make your Java applications more robust and modular.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nested classes in Java are classes that are defined within the scope of another class. They are used to logically group classes that are only used in one place, and they can help increase encapsulation and readability. In Java, there are four types of nested classes: Here is the simplest example: public class Oyster {&nbsp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2169,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[404,406,58,405],"class_list":["post-2136","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-tutorials","tag-classes","tag-inner-classes","tag-java","tag-nested-classes"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=2136"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/2136\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/2169"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=2136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=2136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=2136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}