Java Inner Class

An inner class in Java is a class that is defined within another class. Inner classes have access to the members (fields, methods) of the outer class, even if they are private. Inner classes help logically group classes that are only used in one place, making your code more readable and maintainable.

Types of Inner Classes in Java:

There are four types of inner classes in Java:

  1. Non-static Inner Class (Member Inner Class)
  2. Static Nested Class
  3. Local Inner Class
  4. Anonymous Inner Class

Non-static Inner Class (Member Inner Class):

A non-static inner class is associated with an instance of the outer class. It can access the instance variables and methods of the outer class.


Syntax:

class OuterClass {
    private int outerVar = 10;


    class InnerClass {
        void display() {
            System.out.println("Outer variable: " + outerVar);
        }
    }
}
Example
class Outer {
private int outerValue = 100;
class Inner {
void show() {
System.out.println("Value from Outer class: " + outerValue);
}
}
}
public class Main {
public static void main(String[] args) {
Outer outerObj = new Outer();
Outer.Inner innerObj = outerObj.new Inner();
innerObj.show(); // Accessing the inner class and its method
}
}

Try it yourself


Static Nested Class:

A static nested class is a static member of the outer class. It can only access static members of the outer class. It does not need an instance of the outer class to be instantiated.


Syntax:

class OuterClass {
    static int outerStaticVar = 20;


    static class StaticInnerClass {
        void display() {
            System.out.println("Outer static variable: " + outerStaticVar);
        }
    }
}
Example
class Outer {
static int outerValue = 50;

static class Inner {
void show() {
System.out.println("Outer static variable: " + outerValue);
}
}
}
public class Main {
public static void main(String[] args) {
Outer.Inner innerObj = new Outer.Inner(); // No need for an instance of Outer
innerObj.show();
}
}

Try it yourself


Local Inner Class:

A local inner class is defined inside a method or a block of code, and it can only be used within that method/block. It cannot have access to the instance variables of the outer class unless they are declared final or effectively final.

Example
class Outer {
void display() {
class LocalInner {
void show() {
System.out.println("Inside Local Inner Class");
}
}
LocalInner localInner = new LocalInner();
localInner.show();
}
}
public class Main {

public static void main(String[] args) {
Outer outerObj = new Outer();
outerObj.display(); // Local inner class is used inside the display method
}
}

Try it yourself


Anonymous Inner Class:

An anonymous inner class is a special type of inner class that does not have a name. It is used to create instances of classes that may not be reused elsewhere. These classes are typically used to provide an implementation for interfaces or extend a class in a one-off manner.

  • Abstract 
  • Interfaces
Example1 : Abstract
abstract class Greeting {
abstract void greet();
}
public class Main {
public static void main(String[] args) {
// Anonymous class implementing the Greeting interface
Greeting greeting = new Greeting() {
public void greet() {
System.out.println("Hello from Anonymous Class!");
}
};
greeting.greet(); // Output: Hello from Anonymous Class!
}
}

Try it yourself

Example1 : Interfaces
interface Greeting {
void greet();
}

public class Main {
public static void main(String[] args) {
// Anonymous class implementing the Greeting interface
Greeting greeting = new Greeting() {
public void greet() {
System.out.println("Hello from Anonymous Class!");
}
};
greeting.greet(); // Output: Hello from Anonymous Class!
}
}

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.