Categories: Core JAVATutorial

Member inner class

A class that is declared inside a class but outside a method is known as member inner class.
Invocation of Member Inner class
  1. From within the class
  2. From outside the class

Example of member inner class that is invoked inside a class
In this example, we are invoking the method of member inner class from the display method of Outer class.

class Outer{
   private int value = 70;
   class Inner{
      void show(){
          System.out.println("value is "+value );
      }
    }
 
   void display(){
     Inner in = new Inner();
     in.show();
   }
  public static void main(String args[]){
    Outer obj = new Outer();
    obj.display();
  }
}

output:

See in the directory there are the two following class files after compiling.
Outer.class
Outer$Inner.class

Internal code generated by the compiler for member inner class:
The java compiler creates a class file named Outer$Inner in this case. The Member inner class have the reference of Outer class that is why it can access all the data members of Outer class including private.

import java.io.PrintStream;

class Outer$Inner
{
    final Outer this$0;
    Outer$Inner()
    {   super();
        this$0 = Outer.this;
    }

    void show()
    {
        System.out.println((new StringBuilder()).append("value is ")
                    .append(Outer.access$000(Outer.this)).toString());
    }
 }

Example of member inner class that is invoked outside a class

In this example, we are invoking the show() method of Inner class from outside the outer class i.e. Test class.

//Program of member inner class that is invoked outside a class

class Outer{
  private int value = 70;
  class Inner{
   void show(){System.out.println("value is "+value);}
  }
}

class Test{
  public static void main(String args[]){
    Outer obj = new Outer();
    Outer.Inner in = obj.new Inner();
    in.show();
  }
}

output:

Rules for Inner Classes:
Following properties can be noted about Inner classes:

  • The outer class (the class containing the inner class) can instantiate as many number of inner class objects as it wishes, inside it’s code.
  • If the inner class is public & the containing class as well, then code in some other unrelated class can as well create an instance of the inner class.
  • In above case the inner class can be created as follows:
<OuterClassName> outerObj = new <OuterClassName>(arguments);
outerObj.<InnerClassName> innerObj = outerObj.new <InnerClassName>(arguments);
  • No inner class objects are automatically instantiated with an outer class object.
  • If the inner class is static, then static inner class can be instantiated without an outer class instance, otherwise, the inner class object must be associated with an instance of the outer class.
  • Inner class code has free access to all elements of the outer class object that contains it, by name (no matter what the access level of the elements is), if the inner class has a varible with same name then the outer class’s variable can be access like this:
<OuterClassName>.this.<variableName>
  • The outer class can call even the private methods of the inner class

<<Previous <<   || Index ||   >>Next >>
Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago