Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java Nested Classes & Inner Classes

A class defined inside another class is called nested class.

Suppose there are two classes named ABC & XYZ.
If a class XYZ is defined inside the class ABC, then it is said that the class XYZ is nested inside class ABC.

Java Programming Language Nested class

There are two types of Nested classes

      1. Static nested classes
      2. Non-static nested classes

A class with static access modifier is known as static nested class.

A class without static access modifier is known as non-static nested class.

A nested class can access public, protected as well as private members of an outer class.

Inner classes

A non-static nested class is known as an inner class.
It can access all the variables & methods of its outer class.
There are three types of nested inner classes depending on the location where they are defined.
      1. Member of an enclosing class
      2. Local inner class
      3. Anonymous inner class

Member of an enclosing class

The class defined directly inside an enclosing class is known as a member of an enclosing class.

Syntax

Java Programming Language Member of an enclosing class

Example – I

class OuterClass 
{
    int variable1 = 10;
    public int variable2 = 20;
    protected int variable3 = 30;
    private int variable4 = 40;
	
    public void outerClassMethod1() 
    {
        System.out.println("Outer Class' public Method Accessed");
    }
    
    private void outerClassMethod2() 
    {
        System.out.println("Outer Class' private Method  Accessed");
    }

    class InnerClass
    {
        public void showOuterClassMembers()
        {
            System.out.println("variable1 :: "+variable1);
            System.out.println("variable2 :: "+variable2);
            System.out.println("variable3 :: "+variable3);
            System.out.println("variable4 :: "+variable4);
            outerClassMethod1();
            outerClassMethod2();
        }
    }
}

class Test 
{
    public static void main(String args[])
    {
        OuterClass outObj = new OuterClass();
        OuterClass.InnerClass inObj = outObj.new InnerClass();
        inObj.showOuterClassMembers();
    }
}

Output

variable1 :: 10
variable2 :: 20
variable3 :: 30
variable4 :: 40
Outer Class' public Method Accessed
Outer Class' private Method Accessed

Example – II

class OuterClass2 
{
    int variable1 = 100;
    public int variable2 = 200;
    protected int variable3 = 300;
    private int variable4 = 400;
	
    public void outerClassMethod1() 
    {
        System.out.println("Outer Class' public Method Accessed");	
    }
    
    private void outerClassMethod2() 
    {
        System.out.println("Outer Class' private Method  Accessed");
    }
    
    public void createInnerClassObject() 
    {
        InnerClass inObj = new InnerClass();
        inObj.showOuterClassMembers();
    }

    class InnerClass
    {
        public void showOuterClassMembers()
        {
            System.out.println("variable1 :: "+variable1);
            System.out.println("variable2 :: "+variable2);
            System.out.println("variable3 :: "+variable3);
            System.out.println("variable4 :: "+variable4);
            outerClassMethod1();
            outerClassMethod2();
        }
    }
}

class Test2 
{
     public static void main(String args[])
     {
         OuterClass2 outObj2 = new OuterClass2();
         outObj2.createInnerClassObject();
     }
}

Output

variable1 :: 100
variable2 :: 200
variable3 :: 300
variable4 :: 400
Outer Class' public Method Accessed
Outer Class' private Method Accessed

An Inner class can access public, protected as well as private members of an outer class, but an Outer class cannot access any member of an Inner class.

Example

class OuterClass3 
{
    class InnerClass
    {
        public void innerClassMethod() 
        {
            System.out.println("Inner Class Method");
        }
    }
	
    public void outerClassMethod() 
    {
        innerClasMethod();
    }
}

class Test3 
{
    public static void main(String args[])
    {
        OuterClass3 obj = new OuterClass3();
        obj.outerClassMethod();
    }
}

Java gives compile time error as stated below,
                                  The method innerClassMethod() is undefined for the type OuterClass3
because we are trying to access Inner class member from Outer class.

We can’t declare any static member inside an inner class.

Example

public class OuterClass4 
{
    class InnerClass
    {
        public static void main()
        {
            System.out.println("Inside Static main method");
        }
    }
}

Java gives compile time error as stated below,
                     The method main cannot be declared static; static methods can only be declared in a static or top level type

 

Leave a reply