Inner Classes in Java

Recently I study the topic and sharing the notes for the same.

Inner Classes

Without existing one type of object if there is no chance of existing another type of object then we should go for inner classes.
Note(we can declare anything inside anything means class inside interface or interface inside the class)
Ex: 1 class Car{
class Engine{}
}
Here without Car object, there is no chance of an Engine object.

Ex: 2 Map Interface: A map is a group of key-value pair and each key-value pair is called an entry, without an existing map object there is no chance of existing
Entry object, hence interface entry is defined inside the interface map.

interface Map{
interface Entry{
}
}

The relation between outer and inner class is a Has-a relationship. (Composition or aggregation)

Based on the position of declaration and behaviors all inner classes are divided into 4 types:
1. Normal or Regular Inner class.
2. Method Local inner class: define method-specific repeatedly required functionality.
3. Anonymous inner class: If the requirement is only a single time. (No Constructor, only 1 class extend or only 1 interface implement at a time.)
4. static nested class.

___________________________________________________________________________________________________________________________________________________
1. Normal Inner class:

- If we declare any named class directly inside a class without a static modifier such type of inner class is called Normal or Regular inner class.

Ex: 1 . Access Inner class code from static area of outer class (ex 1 and 3 are same)

class Outer{
class Inner{
public void m1(){
sysout(“Inner class method”);
}
}
public static void main(String[]args){
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.m1();
}
}

Ex: 2 Access Inner class code from instance area of the outer class

class Outer{
class Inner{
public void m1(){
sysout(“Inner class method”);
}
}
public void m2(){
Inner inner = new Inner();
inner.m1();
}
public static void main(String[]args){
Outer outer = new Outer();
outer.m2();
}
}

Ex: 3 Access Inner class code from outside of outer class

class Outer{
class Inner{
public void m1() {
System.out.println(“Inner Class”);
}
}
public void m1() {
System.out.println(“Outer Class”);
}
}

public class Test {

public static void main(String[] args) {
Outer outer = new Outer();
outer.m1();
Outer.Inner inner = outer.new Inner();
inner.m1();

}

}

1. Nesting of inner classes is also possible.
2. we can access instance and static variables of outer class inside the inner class.

___________________________________________________________________________________________________________________________________________________
2. Method Local Inner class:
- Sometimes we declare the class inside a method, such types of classes are called method local inner class.
- Purpose is to define method-specific repeatedly required functionality.
- We can access method local inner classes only inside the method where we declare it, outside we cannot use it.

class Outer{
public void m1(){
class Inner{
public void sum(int x, int y){
sysout(“The Sum is “+(x+y));
}
}
Inner inner = new Inner();
inner.sum(1,2);
inner.sum(3,4);
}
public static void main(String[]args){
Outer outer = new Outer();
outer.m1();
}
}

Notes:
1. We can access local variables of the outer class inside the inner class only if they are final bcz final variable replaced its value at compile time.
2. We can access both instance and static variables inside the inner class if the method is not declared as static.
3. If the method is declared as static then we will not accept instance variables inside the inner class.
4. We cannot declare the static method inside the inner class. (applicable modifier are final, abstract strictfp)

Ex: Here at line 1 we can access i, j,l varables only.
Also if we replace m1() as static then, i and l only.

class Outer{
staic int i = 10;
int j = 20;
public void m1(){
int k = 30;
int final l = 40;
class Inner{
public void m2(){
// Line 1
}
}
}
}

___________________________________________________________________________________________________________________________________________________
3. Anonymous inner classes
- Sometimes we declare inner classes without name such type of classes are called Anonymous inner classes.
Three types are there:
- Anonymous inner class that extends a class.
- Anonymous inner class that implements an interface.
- Anonymous inner class that defined inside the argument.

1. Anonymous inner class that extends a class.
Here the generated .class files are : Popcorn.class, Test.class, Test$1.class (1st anonymous class), Test$2.class(2nd anonymous class)

Popcorn p = new Popcorn();
Popcorn p = new Popcorn(){};
Popcorn p = new Popcorn(){
public void taste(){
sysout(“Sweet”);
}
};

class Popcorn{
public void taste(){
Sysout(“Salty”);
}
}
class Test{
public static void main(String[] args){
Popcorn p = new Popcorn(){

public void taste(){
Sysout(“Spicy”);
}
};
p.taste(); // Spicy

Popcorn p1 = new Popcorn();
p1.taste() // Salty

Popcorn p3 = new Popcorn(){

public void taste(){
Sysout(“Sweet”);
}
};
p3.taste(); //Sweet
}
}

Defining a thread by extending Thread class.

class ThreadDemo{
psvm(String[] args){
Thread thread = new Thread(){
public void run(){
for(int i=0;i<10;i++)
sysout(“Thread called”);
}
};
thread.start();

for(int i=0;i<10;i++){
sysout(“Main called”);
}
}
}

2. Anonymous inner class that implements an interface.

class ThreadDemo{
psvm(String[] args){
Runnable runnable = new Runnable(){
public void run(){
for(int i=0;i<10;i++)
sysout(“Thread called”);
}
};
Thread thread = new Thread(runnable);
thread.start();

for(int i=0;i<10;i++){
sysout(“Main called”);
}
}
}

3. Anonymous inner class that defined inside argument.
class ThreadDemo{
psvm(String[] args){
new Thread(new Runnable(){
public void run(){
for(int i=0;i<10;i++)
sysout(“Thread called”);
}
}).start();

for(int i=0;i<10;i++){
sysout(“Main called”);
}
}
}
___________________________________________________________________________________________________________________________________________________

4. static nested class.
- Sometimes we declare inner classes with a static modifiers, these types of classes are called static nested classes.
- Without an existing outer class object there is the chance of existing nested class object, hence static nested is not strongly associated with the outer class.
- We can access the only static members of the outer class inside the inner class.

class Outer{
static class Inner{
public void m1(){
sysout(“static nested class method”);
}
}

public static void main(String[] args){
Inner inner = new Inner();
inner.m1();
}
}

// if we want to create from outside of class : Outer.Inner i = new Outer.Inner();

Here we can declare a static method inside the inner class(also main method) and also we can invoke static nested class directly from the command prompt.

Thank you for reading!

Happy Coding!

Software Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store