Java Member Inner classA non-static class that is created inside a class but outside a method is called member inner class. It is also known as a regular inner class. It can be declared with access modifiers like public, default, private, and protected. Syntax: Java Member Inner Class ExampleIn this example, we are creating a msg() method in the member inner class that is accessing the private data member of the outer class. TestMemberOuter1.java Test it NowOutput: data is 30 How to instantiate Member Inner class in Java?An object or instance of a member's inner class always exists within an object of its outer class. The new operator is used to create the object of member inner class with slightly different syntax. The general form of syntax to create an object of the member inner class is as follows: Syntax: Example: Here, OuterClassReference is the reference of the outer class followed by a dot which is followed by the new operator. Internal working of Java member inner classThe java compiler creates two class files in the case of the inner class. The class file name of the inner class is "Outer$Inner". If you want to instantiate the inner class, you must have to create the instance of the outer class. In such a case, an instance of inner class is created inside the instance of the outer class. Internal code generated by the compilerThe Java compiler creates a class file named Outer$Inner in this case. The Member inner class has the reference of Outer class that is why it can access all the data members of Outer class including private. Next TopicJava Anonymous Inner class |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India