Home » Core Java » class » Inner class reference

About Ilias Tsagklis

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

Inner class reference

With this example we are going to demonstrate how to make an inner class reference. In short, to make an inner class reference we have followed the above steps:

  • We have created a class InnerClassRef, that contains an inner class C and another inner class D.
  • Class C has a method value() that returns an int value.
  • Class D has a method readLabel() that returns a String value.
  • Class InnerClassRef has two methods, to(String s) that returns a D, and cont() that returns a C. It also has a method boat(String dest) that creates a new C and a new D instance, using cont() and to(String s) methods.
  • We create a new instance of InnerClassRef and call its boat(String s) method with a given String.
  • Then we define the references to the inner classes of InnerClassRef using cont() and to(String s) methods of InnerClassRef.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

public class InnerClassRef {

    public static void main(String[] args) {

  InnerClassRef inner1 = new InnerClassRef();


  InnerClassRef inner2 = new InnerClassRef();


  // Defining references to inner classes:

  InnerClassRef.C c = inner2.cont();

  InnerClassRef.D d = inner2.to("Thessaloniki");

    class C {

  private int i = 11;

  public int value() {

return i;


    class D {

  private String str;

  D(String whereTo) {

str = whereTo;


  String readLabel() {

return str;


    public D to(String s) {

  return new D(s);

    public C cont() {

  return new C();

    public void boat(String dest) {

  C c = cont();

  D d = to(dest);




This was an example of how to make an inner class reference in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

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


Notify of

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

Inline Feedbacks
View all comments