Nested Classes

The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:

                              class Outer_class{  

                                             //code  

                              class Nested_class{  

                                             //code  

                               }  

                              }  

The scope of a nested class is bounded by the scope of its enclosing class. Thus in above example, class Nested_Class does not exist independently of class Outer_Class.

A nested class has access to the members, including private members, of the class in which it is nested. The reverse is also true i.e., the enclosing class can access the members of the nested class.

A nested class is also a member of its enclosing class.

As a member of its enclosing class, a nested class can be declared privatepublicprotected,package private(default),static,abstract or final. 

Nested classes are divided into two categories:

            Inner class and Static nested class

    inner class : An inner class is a non-static nested class.

    In the case of normal or regular inner classes, without an outer class object existing, there cannot be an inner class object. i.e., an object of the inner class is always strongly associated with an outer class object. 

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 outerObject = new OuterClass(); 

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

Static nested class : Nested classes that are declared static are called static nested classes.

In the case of static nested class, Without an outer class object existing, there may be a static nested class object. i.e., an object of a static nested class is not strongly associated with the outer class object.

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

Example 1

// 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 a private member of the 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();

         

    }

}

 Example 2

//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 members

            // 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();

         

    }

}

  


 










Comments

Popular posts from this blog

Enhanced for - loop or for-each loop

Arrays

Java is Strongly typed