Introduction To Nested Class In Java

Introduction

In this article we discuss nested classes (inner classes) in Java.

Nested class

One of the nice and highly advantageous features of Java is the ability to define nested classes, in other words classes within classes. In Java classes are either top-level-classes (a class defined directly within a package) or nested classes (classes defined within top-level-classes).

Syntax:

class MainClass

{

//some coding here

class NestedClass

{

//some coding here

}

}

Advantages

The main advantages of a nested (inner) class are:

  • It shows a special type of relationship, in other words it has the ability to access all all the data members (data members and methods) of the main class including private.
  • They provide easier code because it logically groups classes in only one place.
  • Reduces coding since we need less code to write.

Types

This can de divided into two categories:

  1. static class
  2. non-static class are also called as inner classes.

Static Classes

This class can't refer directly to their instance variables or methods defined in its enclosing class since they use them only by an object reference.

They are accessed by the use of class name:

MainClass.StaticNestedClass

For creating an object, the syntax is:

MainClass.StaticNestedClass nestedObject =new MainClass.StaticNestedClass();

Example

class Main

  {

    static int value=430;

    static class Inner

      {

        void mesg()

          {

            System.out.println("Data value is " + value);

          }

      }

    public static void main(String[] args)

      {

        Main.Inner c=new Main.Inner();

        c.mesg();

      }

  }  

Output

fig-1.jpg

Non-static class (Inner class)

This class refers directly to its instance variables or methods defined in its enclosing class. Also, because an inner class is associated with an instance, it cannot define any static members itself.

The syntax for a non-static class is:

class MainClass

  {

    //Some code here

    class InnerClass

      {

        //some code here

      }

  }

1. Example

In this example, we are invoking the methods of the member Inner1 class from the dsply() method of the Main1 class.

class Main1

  {

    private int value=330;

    class Inner1

      {

        void passmsg()

          {

            System.out.println("value is " + value);

          }

      }

    void dsply()

      {

        Inner1 inr=new Inner1();

        inr.passmsg();

      }

    public static void main(String args[])

      {

        Main1 m=new Main1();

        m.dsply();

      }

  }

Output

fig-2.jpg

2.Example

In this example, we are invoking the messg() mathod of the Inner2 class from outside the class, in other words the Print class.

class Main2

  {

    private int value=430;

    class Inner2

      {

        void messg()

          {

            System.out.println("Vaule is " +value);

          }

      }

  }

class Print

  {

    public static void main(String args[])

      {

        Main2 m2=new Main2();

        Main2.Inner2 inr = m2.new Inner2();

        inr.messg();

      }

  }

Output

fig-3.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all