Java Inner Classes

Java inner class is defined inside the body of another class. Java inner class can be declared private, public, protected, or with default access whereas an outer class can have only public or default access.

Java inner class or nested class is a class which is declared inside the class or interface.

We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.

Additionally, it can access all the members of outer class including private data members and methods.

Syntax of Inner class
class Java_Outer_class{ //code class Java_Inner_class{ //code } }
Advantage of java inner classes

There are basically three advantages of inner classes in java. They are as follows:

  1. Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.

  2. Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.

  3. Code Optimization: It requires less code to write.

static nested class

If the nested class is static, then it’s called static nested class. Static nested classes can access only static members of the outer class. Static nested class is same as any other top-level class and is nested for only packaging convenience.

Static class object can be created with following statement.

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
java inner class

Any non-static nested class is known as inner class in java. Java inner class is associated with the object of the class and they can access all the variables and methods of the outer class.

Since inner classes are associated with instance, we can’t have any static variables in them.

Object of java inner class are part of the outer class object and to create an instance of inner class, we first need to create instance of outer class.

Java inner class can be instantiated like this;

OuterClass outerObject = new OuterClass(); OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Difference between nested class and inner class in Java

Inner class is a part of nested class. Non-static nested classes are known as inner classes.

Types of Nested classes

There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.

  • Non-static nested class (inner class)
    1. Member inner class
    2. Anonymous inner class
    3. Local inner class
  • Static nested class
TypeDescription
Member Inner ClassA class created within class and outside method.
Anonymous Inner ClassA class created for implementing interface or extending class. Its name is decided by the java compiler.
Local Inner ClassA class created within method.
Static Nested ClassA static class created within class.
Nested InterfaceAn interface created within class or interface.
Benefits of Java Inner Class
  1. If a class is useful to only one class, it makes sense to keep it nested and together. It helps in packaging of the classes.

  2. Java inner classes implements encapsulation. Note that inner classes can access outer class private members and at the same time we can hide inner class from outer world.

  3. Keeping the small class within top-level classes places the code closer to where it is used and makes code more readable and maintainable.




Instagram