Sitemap
Perla Pawan Phanieswar

Perla Pawan Phanieswar

I'm a Software Engineer working at a product-based company called Verizon. I write about AI, programming, trending technologies, and tech experiences.

Press enter or click to view image in full size

Inner Classes in Java

7 min readDec 29, 2024

--

Have you ever wondered whether you can access private variables and methods declared in another class that can still be used in a new class?

Yes, you can do that by using something called Inner Classes. In this blog, you will know everything about Inner Classes, their types, practical usage, and some important points about them.

Inner Class

Having a class inside a class is called an Inner class. Here is an example:

public class OuterClass {
// Example of Inner Class
class InnerClass {

}
}

But these are not inner classes

package com.java.InnerClasses;

public class Class1 {
public static void main(String[] args) {
System.out.println("Class1");
}
}

class Class2 {
public static void main(String[] args) {
System.out.println("Class2");
}

}

class Class3 {
public static void main(String[] args) {
System.out.println("Class3");
}
}

If you execute the above program, the IDE will ask you which program it should run, and it will be like this, showing you different options.

Press enter or click to view image in full size

Inner Classes have been introduced for better restriction of properties of a class and also to limit the scope of each attribute in the class.
Also, by using Inner Classes, the inner class can access the private attributes of the Outer Class.

All Inner Classes, must have unique names, and we can also add any number of Inner Classes for a class.

The naming convention of each Inner Class file in the bin folder will be
“Outer_Class_Name$Inner_Class_Name”.

Also, for every Inner Class, in its .class file, we will have a Nest Host section.

Press enter or click to view image in full size
Nest Host Section in the .class file

So basically, if you want to know whether a class is Inner Class or Outer Class, you can see its .class files, Nested Components. For Inner Class, it will have something called Nest Host, for Outer Class it will have something called Nested Memebers.

All the Inner Classes will have a default access modifier. But we can also give public, private, protected also. The classes can be declared static, abstract, or final. Each Inner Class should not have a main method.

package com.java.InnerClasses;

public class Class1 {
public static void main(String[] args) {
System.out.println("Class1");
}

public class InnerClass1 {

}

private class InnerClass2 {

}

protected class InnerClass3 {

}

static class InnerClass4 {

}

abstract class InnerClass5 {

}

final class InnerClass6 {

}
}

Now that you understand what an Inner Class is, let me tell you about the different types of Inner Classes Java provides us.
There are 4 types of Inner Classes in Java, they are:

  1. Non-Static Inner Classes
  2. Static Inner Classes
  3. Local Inner Classes
  4. Anonymous Inner Classes

Non-Static Inner Class

It is a class defined inside another class but outside any method or constructor.

Get Perla Pawan Phanieswar’s stories in your inbox

Join Medium for free to get updates from this writer.

An example of it is:

package com.java.InnerClasses;

// Accessing inner class code.

public class OuterClass {
public static void main(String[] args) {
// Way 1
OuterClass.InnerClass innerClass = new OuterClass().new InnerClass();
innerClass.display();
// Way 2
OuterClass outerClass = new OuterClass();
InnerClass innerClass = outerClass.new InnerClass();
innerClass.display();
// Way 3
new OuterClass().new InnerClass().display();
}

public class InnerClass {
public void display() {
System.out.println("InnerClass display method");
}
}
}

Key Characteristics of Non-static Inner Classes:

  • An instance of the outer class is linked to a non-static inner class. It has access to every member of the outer class, including private members.
  • To instantiate a non-static inner class, you need to first create an instance of the outer class. The inner class cannot exist on its own.
  • The non-static inner class has direct access to the instance variables and methods of the outer class, even private ones.
  • Within the inner class, this refers to the current inner class object.
    Look at this example for better understanding
package com.java.InnerClasses;

public class OuterClass {
int number = 101;
public static void main(String[] args) {
OuterClass.InnerClass innerClass = new OuterClass().new InnerClass();
innerClass.display();
}

public class InnerClass {
int number = 102;
public void display() {
int number = 103;
System.out.println(number); // outputs 103
System.out.println(this.number); // outputs 102
System.out.println(OuterClass.this.number); // outputs 101
}
}
}

All Inner Classes can:

  • Extend Non-Abstract Class
package com.java.InnerClasses;

public class OuterClass {
public static void main(String[] args) {
OuterClass.InnerClass innerClass = new OuterClass().new InnerClass();
innerClass.display();
}
public class InnerClass extends NonAbstractClass {
public void display() {
System.out.println("Inner Class Display method");
nonAbstractMethod();
}
}
}

class NonAbstractClass {
public void nonAbstractMethod() {
System.out.println("NonAbstract method");
}
}
  • Extend Abstract Class by providing an implementation for all unimplemented methods.
package com.java.InnerClasses;

public class OuterClass {
public static void main(String[] args) {
OuterClass.InnerClass innerClass = new OuterClass().new InnerClass();
innerClass.display();
}
public class InnerClass extends AbstractClass {
public void display() {
System.out.println("Inner Class Display method");

}

@Override
public void unimplementedMethod() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'unimplementedMethod'");
}

}
}


public abstract class AbstractClass {
public abstract void unimplementedMethod();
}
  • Implement an Interface by providing an implementation for all unimplemented methods.
package com.java.InnerClasses;

public class OuterClass {
public static void main(String[] args) {
OuterClass.InnerClass innerClass = new OuterClass().new InnerClass();
innerClass.display();
}
public class InnerClass implements Interface {
public void display() {
System.out.println("Inner Class Display method");
}

@Override
public void unImplementedMethod1() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'unImplementedMethod1'");
}

@Override
public void unImplementedMethod2() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'unImplementedMethod2'");
}

}
}

interface Interface {
void unImplementedMethod1();
void unImplementedMethod2();
}
  • Extend other Inner Classes.
package com.java.InnerClasses;

public class OuterClass {
public static void main(String[] args) {
OuterClass.InnerClass1 innerClass = new OuterClass().new InnerClass1();
innerClass.display();
}
public class InnerClass1 extends InnerClass2 {
public void display() {
System.out.println("Inner Class1 Display method");
print();

}
}
public class InnerClass2 {
public void print() {
System.out.println("Inner Class2 Print method");

}
}
}

Static Inner Classes

A static inner class (also known as a static nested class) is a class that is declared inside another class but is marked with the static keyword.

It can be accessed without an instance of the outer class, unlike regular inner classes.

Key Features of Static Inner Classes:

  1. Independent of Outer Class Instances: Since it’s static, you don’t need an instance of the outer class to create an object of the static inner class.
  2. Cannot Access Non-static Members of Outer Class: A static inner class can only access the static members (variables and methods) of the outer class directly. It cannot access instance variables or methods without an object of the outer class.
  3. Useful for Utility Classes: Static inner classes are often used for helper or utility classes that are only relevant to the outer class.
package com.java.InnerClasses;

class OuterClass {
private static String outerStaticVar = "Outer Static Variable";
private String outerInstanceVar = "Outer Instance Variable";

public static void main(String[] args) {
OuterClass.StaticInnerClass inner = new OuterClass.StaticInnerClass();
inner.display();
}

static class StaticInnerClass {
void display() {
System.out.println("Accessing outer static variable: " + outerStaticVar);
// System.out.println(outerInstanceVar); // This would cause a compilation error
}
}
}

Local Inner Classes

A Local Inner Class is a class declared within a block, inside a method, constructor, or even an initialization block. Because of its scope, a local inner class is not accessible outside the block in which it is declared. It’s a way to encapsulate functionalities that are needed only within a specific method or block, keeping the code more organised and less exposed.

Only abstract, final keywords can be used for these classes. These local classes can’t have static methods. Local Inner Class objects can be created only in the block of code where you declare them.

The Local Inner Class can possess accessibility to every variable and method outside the class scope and also has access to private properties of the Outer Class. But the Outer Class can’t access anything that is created inside the Local Inner Class.

package com.java.InnerClasses;

public class OuterClass {

int number = 101;
static int number1 = 102;
private static String name = "John";

public static void main(String[] args) {

class InnerClass {
public void display() {
System.out.println("Display Method");
// System.out.print(number); // can't make static reference
System.out.println(number1);
System.out.println(name);
}
}

InnerClass innerClass = new InnerClass();
innerClass.display();

}

public void method() {
// throws error
// InnerClass innerClass = new InnerClass();
}
}

Anonymous Inner Classes

Anonymous Inner Class is a type of local inner class without a name. It is used to instantiate objects of classes or interfaces with slight changes or custom behaviour, especially when a full class declaration is not necessary. Anonymous inner classes are often used when implementing interfaces or abstract classes, especially in cases where the implementation is needed in only one place.

Anonymous Classes naming convention in bin folder will be like.
Outer_Class_Name&1.class

They are used to override any method of a class without extending. And to implement unimplemented methods of Abstract classes and Interfaces.
We will not create an object for abstract classes or interfaces, but using anonymous classes, we can create an object for them with the type of that interface/abstract class name.

Example of an Anonymous Inner Class

package com.java.InnerClasses;

public class OuterClass {
public static void main(String[] args) {
ClassA classA = new ClassA() {
public void display() {
System.out.println("Updated Display Method");
}
};
classA.display();
}

}

class ClassA {
public void display() {
System.out.println("Display Method");
}
}

Thanks for reading this blog. I hope you have understood about Java Inner Classes.

Check out my other blogs for some more advanced concepts in Java and to get a deep understanding of them.
https://medium.com/@pawanphanieswar

Happy Learning!

--

--

Written by Perla Pawan Phanieswar

I'm a Software Engineer working at a product-based company called Verizon. I write about AI, programming, trending technologies, and tech experiences.