Skip to content

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

  • by

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 the outer “OuterClassName” and the other is the inner is “InnerClassName” or nested class.

class OuterClassName {
   class InnerClassName {
   }
}

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 instances for all types of access modifiers.

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

class OuterClass {

    // Nested inner class
    private class InnerClass {
        public void printMsg() {
            System.out.println("This is a Nested inner class");
        }
    }

    // Accessing  inner class from the method within
    void display_Inner() {
        InnerClass inner = new InnerClass();
        inner.printMsg();
    }
}

public class MyClass {

    public static void main(String args[]) {
        // Creating object of Outer Class
        OuterClass outer = new OuterClass();

        // Accessing the method.
        outer.display_Inner();
    }
}

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’s 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.

abstract class AnonymousInner {
    public abstract void myMethod();
}

class OuterClasses {

    public static void main(String args[]) {
        AnonymousInner inner = new AnonymousInner() {
            public void myMethod() {
                System.out.println("Anonymous inner class Examples");
            }
        };
        inner.myMethod();
    }
}

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 an example of Java inner static class. It’s very easy to create a static class just to use a “static keyword” before class.

class OuterClass {

    // static Nested inner class
    static class InnerClass {
        public void printMsg() {
            System.out.println("This is a Static Nested inner class");
        }
    }
}

public class MyClass {

    public static void main(String args[]) {
        // Creating object of Outer Class
        OuterClass.InnerClass outerIn = new OuterClass.InnerClass();

        // Accessing the method.
        outerIn.printMsg();
    }
}

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.

class LocalInner {

    private int number = 977;//instance variable

    void display() {
        class Local {
            void msg() {
                System.out.println("Local number " + number);
            }
        }
        Local l = new Local();
        l.msg();
    }

    public static void main(String args[]) {
        //creating object
        LocalInner localInner = new LocalInner();
        localInner.display();
    }

}

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.

interface MyInterface {
    void show();

    interface Message {
        void msg();
    }
}

class NestedInterface implements MyInterface.Message {
    public void msg() {
        System.out.println("Java Nested interface Example");
    }

    public static void main(String args[]) {
        MyInterface.Message message = new NestedInterface();
        message.msg();
    }
}

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.

MyOuter.MyInner inClass = outClass.new MyInner();

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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading