Provide an example and explanation of an inner class in Java

First off, we want to clarify that when we discuss inner classes in this article, we are not referring to Anonymous inner classes or Argument defined anonymous inner classes. That’s important to clarify, because here we are only talking about “regular” inner classes. And both regular anonymous inner classes and argument defined anonymous inner classes are a special form of inner classes. In order to understand those in more depth, and to see how they are different from normal inner classes, you should definitely read the articles we linked to. But, you can certainly understand this tutorial and examples without knowing what anonymous inner classes are, and you can read those articles later.

Let’s start out this tutorial on inner classes in Java with some very simple code to see what an inner class looks like:

Inner Class Example Code in Java

class OuterClass {

  class InnerClass {  }

}

The syntax is very simple. Now, let’s say that we compile the class using the following command:

javac OuterClass.java

What is interesting here is that when we compile a file that contains an inner class, we actually end up with two class files and not one. So, these two class files are what we end up with:

OuterClass.class
OuterClass$InnerClass.class

Because of the fact that the inner class is technically a separate class, a separate class file will be created for it as well – which can see with the “OuterClass$InnerClass.class” class file. As you can see, the dollar sign is used to separate the inner class name from the outer class name.

An inner class can only be accessed through an instance of the outer class

Even though a non-static inner class has a separate class file created for it when the outer class is compiled, that does not mean that we can just access the inner class like any other normal class.

So, for example, let’s say we try to execute the class file generated for the inner class by running this command – because we think that it will run the main() method of the inner class :

javac OuterClass$InnerClass

Can an inner class have static members?

But, we will not be allowed to run the command above, because an inner class can not have any static members at all – and that includes “public static void main()”. This is because we would not be able to access the instance variables of the outer class from within a static context of the inner class.

And, if there were a static method inside the “inner class” it wouldn’t even be an inner class – it would technically have to be declared with the static modifier and therefore it would be a static nested class for reasons you can read about here: Difference between inner and nested classes.

So, this means that the only way that we can access an inner class is by creating an actual instance of the outer class. And this also means that we can only access the inner class at runtime when we already have an instance of the outer class that we can use to access the inner class.

An example and tutorial of a simple inner class

Let’s go through an example of a simple inner class in Java that accesses a private member variable of the outer class – note that this is possible because an inner class has access to everything in the outer class. Take a look at the code below:

class OuterClass {

private int privInt = 10;
  

 class InnerClass {  
   
  public void accessOuter() {

  System.out.println("The outer class's
  privInt is " + privInt);

  } 

 }


}

In the code above, you can see that we have highlighted the inner class code in red. And, you can clearly see that the inner class also has access to a private instance variable of the outer class. This is because an inner class is just like any other member of the outer class – even normal instance methods.

How to create an instance of an inner class in Java

Now, let’s go through an example of how to create an inner class in Java. Take a look at this code:

class OuterClass {

 private int privInt = 10;
 
  
 public void createInnerClass() {

 InnerClass inClass = new InnerClass();
 inClass.accessOuter();
 }
 

 class InnerClass {  
   
  public void accessOuter() {

  System.out.println("The outer class's
  privInt is " + privInt);

  } 

 }


}

The code above is an example of how to create an instance of a class from inside the outer class. We highlighted the outer class method in red which creates an instance of the inner class and calls an inner class method. The code is pretty straightforward.

The code above works because it is inside an instance method of the outer class and there is already an implied instance of the outer class, which would be the instance that calls the createInnerClass() instance method.

But now the question is how can we create an instance of our inner class from some code that is outside the outer class. Or, more specific to our example code, how can we create an instance of the InnerClass from some code that is not inside the OuterClass code? Let’s go through some code that does exactly that.

How to create an inner class object from outside the outer class code

This is what the code would look like in order to create an inner class object in some code that is outside the outer class code:

public static void main(String[] args) {
//create instance of outer class first:
 OuterClass outClass = new OuterClass();
 
OuterClass.InnerClass inner = outClass.new InnerClass(); 

 inner.accessOuter();
}

Note that the code above would be exactly the same whether the main() method is inside the outer class (because main is a static method) or even in some other class. But, one thing to take note of is the fact that some other class could only run the code if it has access to our outer class (called OuterClass in our code further above). But, OuterClass will have default, or package access since we did not specify any access modifiers when we declared OuterClass. This essentially means that any class within the same package as OuterClass will be able to run the code above without any issues.

Also, a shorter version of the code above – which allows us to instantiate the inner class using just one statement – is right here:

public static void main(String[] args) {
 
OuterClass.InnerClass inner =  
new OuterClass().new InnerClass(); 

 inner.accessOuter();
}

How does the code above work exactly? Well, we are obviously creating both an outer class and an inner class instance within the same statement. But, the outer class instance is not given a name since we only want the inner class reference. And, you can think of it as calling a method on the outer class instance, where that method creates an instance of the inner class, but it uses the “new” keyword to create the inner class instance.

You should be aware that this is the only time you will see code like this – where we use the “new” keyword on an existing instance, instead of using the “new” keyword to create a brand new instance.

Summary of different ways to create inner class objects

Let’s summarize the two different ways to create inner class instances, and also discuss how they are different:

  • If you want to create an inner class instance from inside the outer class code, then you can instantiate the class using the normal syntax:

    InnerClass inClass = new InnerClass();
    
  • If you want to create an inner class code outside of the outer class code, or inside a static method that is still a member of the outer class, then you must use a reference to the outer class, like this:

    OuterClass.InnerClass inner =  new 
    OuterClass().new InnerClass(); 
    

    Or you can use the longer syntax like this:

    OuterClass outClass = new OuterClass();
    
    OuterClass.InnerClass inner = 
    outClass.new InnerClass(); 
    

And that is the end of our tutorial on and examples of inner classes. If you have any questions, please leave a comment below!

Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

Subscribe to our newsletter for more free interview questions.

14 thoughts on “Java Inner Class Example”

WordPress › Error

There has been a critical error on your website.

Learn more about debugging in WordPress.