Inner class instance example

In this example we shall show you how to call an inner classe’s instance in a class. To call an inner class instance in a class, we must first create an instance of the outer class, and then create an instance of the inner class, as described in the example:

  • We have created a class, InnerClassInstance that has two inner classes, InnerClass1 and InnerClass2.
  • InnerClass1 has an int field initialized to a value, and a method int value() that returns its int field.
  • InnerClass2 has a String field, a constructor where it initializes its String field to the given String, and a method String readLabel() that returns its String field.
  • We create a new instance of InnerClassInstance.
  • Then we create a new instance of each one of the inner classes, using the InnerClassInstance object and its inner classe’s constructors,

as described in the code snippet below.  

package com.javacodegeeks.snippets.core;

public class InnerClassInstance {

    class InnerClass1 {

  private int i = 11;

  public int value() {

return i;


    class InnerClass2 {

  private String dest;

  InnerClass2(String whereTo) {

dest = whereTo;


  String readLabel() {

return dest;


    public static void main(String[] args) {


  InnerClassInstance p = new InnerClassInstance();


  // Must use instance of outer class

  // to create an instances of the inner class:

  InnerClassInstance.InnerClass1 c = InnerClass1();

  InnerClassInstance.InnerClass2 d = InnerClass2("Greece");

This was an example of how to call an inner classe’s instance in a class in Java.

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button