Inner Classes

We have discussed access modifiers, exceptions, features of Java 8 in previous Java related posts. We will discuss ‘Inner classes’ which is a part of SCJP/OCJP exam objective in this post. Inner classes are part of the code that often stumps a person who is taking the SCJP/OCJP exam.

Definition of Inner classes:

Inner classes are classes which are nested within the outer classes. Why is this done? This is done to promote good object oriented design principles.   According to the Oracle documentation website, inner classes promote:

  1. “Encapsulation
  2. Readable and maintainable code
  3. Logical grouping of classes which is used only in one place”

(Nested Classes)

We will see how these points come into play by observing some examples below.

Types of Inner classes:

Inner classes can be categorized into four types and they are

  1. Inner classes
  2. Method-local inner class
  3. Anonymous inner class
  4. Static nested class

 

Inner classes:

We will discuss the “plain” inner class in this post. There are two crucial points when dealing with Inner classes.

Point 1: Since the inner class is also a member of the outer class, it will have access to all the methods and variables of outer class even if they are declared private.  This is the point that reiterates the concept of encapsulation which will be dealt with in the next post.

Illustrating a small example, if an inner class is defined the following way:

class OuterClass{
private int x=5;
class Inner{
}
}

Class Inner will have access to the private integer variable ‘x’.

Invoking inner classes:
 Next we will see how to invoke inner classes in our code.  This brings us to the next point.

Point 2: All inner classes exist only within an outer class. An instance of an inner class can only exist if an instance of outer class exists. Inner classes do not exist alone!

Example:

Consider this code which shows how to instantiate an inner class from a method in the outer class. Here an instance of the outer class is always available by means of the “this” variable.

This program instantiates the inner class and calls a method in the inner class which prints the message “Hello world” to the console.

// Program to create an instance of inner class from the outer class
package whizlabs;
public class example_inner {
//Outer class method                                
void add(){
example_inner.eg e1=this.new eg();//Inner class is instantiated
e1.sample();
     }
//Inner class
public class eg{
   public void sample(){
     System.out.println("Hello world");
    }
 }
public static void main(String args[]){
//Outer class instantiation
  example_inner e=new example_inner();
  e.add();
     }
}

In the example above, the:

Outer class is: example_inner
Outer class method: add()
Inner class is: eg
Inner class method: sample()

This is the line of code that creates an instance of the inner class from the outer class.

example_inner.eg e1=this.new eg();//Inner class is instantiated

The above code can also be re-written the following way as the “this” variable is always present in the outer class (outside ‘static’ methods)

eg e1=new eg();

Note the way the inner class is instantiated,

    this.new eg();

The output of the program as expected is listed below:

Hello world

We saw plain regular inner classes in this post. We will explore more about inner classes in subsequent posts.

Bibliography
Nested Classes. (n.d.). Retrieved Feb 19, 2015, from Java Tutorial – Oracle documentation: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top