DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Types of Nested Classes in Java

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.

Kun Zhou user avatar by
Kun Zhou
·
Sep. 03, 18 · Tutorial
Like (13)
Save
Tweet
Share
18.77K Views

Join the DZone community and get the full member experience.

Join For Free

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
Java (programming language)

Published at DZone with permission of Kun Zhou. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Quest for REST
  • How To Create and Edit Excel XLSX Documents in Java
  • ChatGPT: The Unexpected API Test Automation Help
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: