Java inner class | Java nested class | Types | Anonymous, Static, Local

Defining one class into another class is called a Java inner class or Java nested class. An inner class can be the default, public, protected, or private access modifier. But for the outer class, it’s only can public or default. Java inner class constructor is called with a new object of the inner class created.

In this tutorial you will learn about a Java nested inner class and types with how to create and access all types of examples.

Java inner class example Java nested class Types Anonymous, Static, Local

Syntax

This is the basic syntax of the nested inner class. Where One class is outer “OuterClassName” and the other is inner is “InnerClassName” or nested class.

Types of inner classes in java

There are 4 types of nested inner class in Java:

Types of inner classes in java nested
  • A nested Inner classes (Non-static nested class)
  • Anonymous inner class (Non-static nested class)
  • Method Local inner class (Non-static nested class)
  • Static nested classes

We will also learn about a Nested Interface in java.

Java Nested Inner class Example

Here is an example of how to define the Nested inner class. An inner class can access outer class variables and instance for all types of access modifiers.

Look at below Java inner class examples (program/code). How to create it and accesses inner class.

Output: This is a Nested inner class

Anonymous inner classes

A without name classes is called the Anonymous inner class in java. It used if you need to override the method of class or interface. There are 2 ways to define Anonymous inner class.

  1. Class (may be abstract or concrete).
  2. Interface

Let’s see the example How to create a Java Anonymous inner class using class. For Interface we will post a separate detailed tutorial.

Output: Anonymous inner class Examples

Static nested classes

Java’s inner static class or Static nested class is logically and technically not an inner class because adding a static keyword in a class represents a member of the outer class. It can’t access non-static data members and methods.

A static class can be accessed by outer class name.

Looked below example of Java inner static class. It’s very easy to create a static class just use a “static keyword” before class.

Output: This is a Static Nested inner class

Method Local inner classes

You know we can write a Java program where a class within a method. This type of class called a Methods local inner class. Same as local variables and fields in methods, the local inner class limited scope only in a method.

Output: Local number 977

Nested Interface in java

So come the last part of this chapter, nested Interface.

Which interface is declared within another interface or class is known as a nested interface. It must be referred to by the outer interface or class. nested Interface can’t be accessed directly.

Output: Java Nested interface Example

Use of inner class in java

Here is some Benefits/Advantage of using a nested inner class in java:

  1. Nested classes (inner class) can access all the members (data members and methods) of the outer class including private.
  2. Nested inner classes are used to develop a more readable and maintainable code because it logically groups classes and interfaces in one place only.
  3. Code Optimization: It requires less code to write.

Question:  What is a Local inner class or Local class in java?

Answer:Method Local inner classes is called a Local inner class, as above check this example.

Question: Is there a constructor associated with nested classes?

Answer: The Inner class constructor is only called when you have created an object of it.

The nested class constructor does not invoke until you create an object. Even static blocks are executed, But instance blocks and constructors are not invoked.

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java inner class Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.