Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Types of Nested Classes in Java

DZone 's Guide to

Types of Nested Classes in Java

Want to learn more about nested classes in Java? Check out this tutorial on how to define a class within another class through inner and static nested classes.

· Java Zone ·
Free Resource

In Java, it is possible to define a class within another class. These such classes are known as the nested classes. They enable you to logically group classes that are only used in one place. Thus, this increases the use of encapsulation and creates a more readable and maintainable code.

  • The scope of a nested class is bounded by the scope of its outer class. Thus, in the above example, the class NestedClass  does not exist independently from the class OuterClass.
  • A nested class has access to the members, including private members, of the outer class. And the reverse also is true.
  • A nested class is also a member of its outer class.
  • As a member of its outer class, a nested class can be declared private, public, protected, or package private (default).
  • Nested classes are divided into two categories:
    1.  static nested class: Nested classes that are declared static are called static nested classes.
    2.  inner class: An inner class is a non-static nested class.

Syntax

class OuterClass
{
...
    class NestedClass
    {
        ...
    }
}


Static Nested Classes

OuterClass.StaticNestedClass nestedObject =
     new OuterClass.StaticNestedClass();
// Java program to demonstrate accessing 
// a static nested class

// outer class
class OuterClass
{ 
    // static member
    static int outer_x = 10;

    // instance(non-static) member
    int outer_y = 20;

    // private member
    private static int outer_private = 30;

    // static nested class
    static class StaticNestedClass
    {
        void display()
        {
            // can access static member of outer class
            System.out.println("outer_x = " + outer_x);

            // can access display private static member of outer class
            System.out.println("outer_private = " + outer_private);

            // The following statement will give compilation error
            // as static nested class cannot directly access non-static membera
            // System.out.println("outer_y = " + outer_y);

        }
    }
}

// Driver class
public class StaticNestedClassDemo
{
    public static void main(String[] args)
    {
        // accessing a static nested class
        OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

        nestedObject.display();

    }
}


Output:

outer_x = 10
outer_private = 30


Inner Classes

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();


There are two special kinds of inner classes :

// Java program to demonstrate accessing
// a inner class

// outer class
class OuterClass
{ 
    // static member
    static int outer_x = 10;

    // instance(non-static) member
    int outer_y = 20;

    // private member
    private int outer_private = 30;

    // inner class
    class InnerClass
    {
        void display()
        {
            // can access static member of outer class
            System.out.println("outer_x = " + outer_x);

            // can also access non-static member of outer class
            System.out.println("outer_y = " + outer_y);

            // can also access private member of outer class
            System.out.println("outer_private = " + outer_private);

        }
    }
}

// Driver class
public class InnerClassDemo
{
    public static void main(String[] args)
    {
        // accessing an inner class
        OuterClass outerObject = new OuterClass();
        OuterClass.InnerClass innerObject = outerObject.new InnerClass();

        innerObject.display();

    }
}


Output:

outer_x = 10
outer_y = 20
outer_private = 30


Difference between static and inner(non-static nested) classes:

  • Static nested classes do not directly have access to other members (non-static variables and methods) of the outer class, because as it is static, it must access the non-static members of its enclosing class through an object. That is, it cannot refer to non-static members of its enclosing class directly.  
  • If the nested class does not access any of the variables of the enclosing class, it can be made static. The advantage of this is that you do not need an enclosing instance of the outer class to use the nested class.
  • Non-static nested classes (inner classes) have access to all members (static and non-static variables and methods, including private) of its outer class and may refer to them directly in the same way that other non-static members of the outer class do. But it can not  contain static members
Topics:
java ,nested class ,encapsulation

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}