知方号

知方号

Nested Classes in Java

In Java, it is possible to define a class within another class, such classes are known as 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 more readable and maintainable code.

The scope of a nested class is bounded by the scope of its enclosing class. Thus in the below example, the class NestedClass does not exist independently of the class OuterClass.A nested class has access to the members, including private members, of the class in which it is nested. But the enclosing class does not have access to the member 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 private, public, protected, or package-private(default).Nested classes are divided into two categories:static nested class: Nested classes that are declared static are called static nested classes.inner class: An inner class is a non-static nested class.

Syntax:

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

Static nested classes

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. But 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. As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference. They are accessed using the enclosing class name.

OuterClass.StaticNestedClass

For example, to create an object for the static nested class, use this syntax:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();Below is the implementation of the above method:Java

// Java program to demonstrate accessing// a static nested class // outer classclass 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 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);                         // Therefore create object of the outer class               // to access the non-static member              OuterClass out = new OuterClass();              System.out.println("outer_y = " + out.outer_y);                              }    }} // Driver classpublic class StaticNestedClassDemo {    public static void main(String[] args)    {        // accessing a static nested class        OuterClass.StaticNestedClass nestedObject            = new OuterClass.StaticNestedClass();         nestedObject.display();    }} Outputouter_x = 10outer_private = 30outer_y = 20Inner 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 :

Local inner classesAnonymous inner classesJava

// Java program to demonstrate accessing// a inner class // outer classclass 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 classpublic class InnerClassDemo {    public static void main(String[] args)    {        // accessing an inner class        OuterClass outerObject = new OuterClass();               OuterClass.InnerClass innerObject            = outerObject.new InnerClass();         innerObject.display();    }} Outputouter_x = 10outer_y = 20outer_private = 30Comparison between a normal or regular class and a static nested classS.No.Normal/Regular inner classStatic nested class1.Without an outer class object existing, there cannot be an inner class object. That is, the inner class object is always associated with the outer class object.Without an outer class object existing, there may be a static nested class object. That is, a static nested class object is not associated with the outer class object.2.As the main() method can’t be declared, the regular inner class can’t be invoked directly from the command prompt.As the main() method can be declared, the static nested class can be invoked directly from the command prompt.3. Both static and non-static members of the outer class can be accessed directly.Only a static member of an outer class can be accessed directly. GeeksforGeeks Improve Previous Article Anonymous Inner Class in Java Next Article Java.util.Objects class in Java

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。