Java Inner Class With Examples

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 Nested classes are divided into two types.

  1. static nested class

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

    A static class object can be created with the following statement.

  2. 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 the instance, we can’t have any static variables in them.

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

    Java inner class can be instantiated like this;

There are two special kinds of Java inner classes.

  1. local inner class

    If a class is defined in a method body, it’s known as local inner class.

    Since the local inner class is not associated with Object, we can’t use private, public or protected access modifiers with it. The only allowed modifiers are abstract or final.

    A local inner class can access all the members of the enclosing class and local final variables in the scope it’s defined. Additionally, it can also access a non-final local variable of the method in which it is defined, but it cannot modify them. So if you try to print non-final local variable’s value it will be allowed but if you try to change its value from inside method local inner class, you will get compile time Error.

    Local inner class can be defined as:

    We can define a local inner class inside any block too, such as static block, if-else block etc. However, in this case, the scope of the class will be very limited.

  2. anonymous inner class

    A local inner class without name is known as anonymous inner class. An anonymous class is defined and instantiated in a single statement.

    Anonymous inner class always extend a class or implement an interface. Since an anonymous class has no name, it is not possible to define a constructor for an anonymous class.

    Anonymous inner classes are accessible only at the point where it is defined.
    It’s a bit hard to define how to create an anonymous inner class, we will see it’s real-time usage in the test program below.

Here is a java class showing how to define java inner class, static nested class, local inner class, and an anonymous inner class.

OuterClass.java

Here is the test program showing how to instantiate and use the inner class in java.

InnerClassTest.java

Here is the output of the above java inner class example program.

Notice that when OuterClass is compiled, separate class files are created for the inner class, local inner class, and static nested class.

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 the 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 the code more readable and maintainable.

That’s all for java inner class.

By admin

Leave a Reply

%d bloggers like this: