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:
- “Encapsulation
- Readable and maintainable code
- 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
- Inner classes
- Method-local inner class
- Anonymous inner class
- 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
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021