class

Reference to an inner class

This is an example of how to make a reference to an inner class. In order to make a reference to an inner class we have created a class with inner classes, as described in the steps below:

  • We have created a class, InnerClass1 that has an int field and a method int value() that returns an int field.
  • We have also created a class, InnerClass2 that has a String field, and a constructor that initializes its String field to a given String. It has a method String readLabel() that returns the String field of the class.
  • Both classes are inner classes of ReferenceInnerClass.
  • The class has a method InnerClass2 to(String s) that returns a new InnerClass2 object initialized with the given String field.
  • It also has a method InnerClass1 cont() that returns a new InnerClass1 object initialized by its default constructor.
  • Another method of ReferenceInnerClass is void ship(String dest), where it calls cont() method to get a new InnerClsas1 object and then to(String s) to get a new InnerClass2 object and prints the the field of InnerClass2 using its readLabel() method.
  • We create a new instance of ReferenceInnerClass, and call its ship(String dest) method with a given String.
  • Then we create a new ReferenceInnerClass object, and follow the same steps as ship(String dest) method, but calling the cont() and to(String s) methods of the inner classes.

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

package com.javacodegeeks.snippets.core;

public class ReferenceInnerClass {

    class InnerClass1 {


  private int i = 11;


  public int value() {


return i;

  }
    }

    class InnerClass2 {


  private String destination;


  InnerClass2(String where) {


destination = where;

  }


  String readLabel() {


return destination;

  }
    }

    public InnerClass2 to(String s) {

  return new InnerClass2(s);
    }

    public InnerClass1 cont() {

  return new InnerClass1();
    }

    public void ship(String dest) {

  InnerClass1 c = cont();

  InnerClass2 d = to(dest);

  System.out.println(d.readLabel());
    }

    public static void main(String[] args) {

  ReferenceInnerClass p = new ReferenceInnerClass();

  p.ship("Athens");

  ReferenceInnerClass q = new ReferenceInnerClass();

  // Defining references to inner classes:

  ReferenceInnerClass.InnerClass1 c = q.cont();

  ReferenceInnerClass.InnerClass2 d = q.to("Thessaloniki");
    }
}

  
This was an example of how to make a reference to an inner 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

 

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button