Java-nested Classes

Source: Internet
Author: User

(This article for reference: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html)

Nested Classes
class Outerclass {    ...     class Staticnestedclass {        ...    }    ......     class innerclass {    ...}
Defined

Nested Classes (nested classes) are divided into two types:non-static and static, which is often referred to as inner classes, which is called static nested Classes.

Scopes and access scopes

Both nested classes are internal members of their enclosing class , andnon-static classes can refer to the private member of the wrapper class. The static classes does not have this ability. Both of the nested classes can be declared as public/protected/private/package private.

Action scenario (due to the high generalization of the text, direct reference)
    • It is a-a-logically grouping classes that's only used in one place.
    • It increases encapsulation.
    • It can leads to more readable and maintainable code.
Static Nested Classes
    • static Nested classes cannot invoke the method/member of its outer class directly, it must be accessed through an instance of an external class (at this point, thestatic inner class behaves as if it were a Top-level class)
    • Create a static nested class instance in the following way
      New Outerclass.staticnestedclass ();
Inner Classes
    • As with instance methods and variables, a inner class is associated with a instance of its enclosing class and have Direc T access to this object ' s methods and fields.
    • Cause an inner class was associated with a instance, it cannot define any static members itself.
    • An instance of Innerclass can exist only within an instance of Outerclass and have direct access to the M Ethods and fields of its enclosing instance.
    • Create a inner class instance in the following way
      Outerclass.innerclass InnerObject = Outerobject. New Innerclass ();
    • There are two special types of inner classes:local classes and anonymous class
Shadowing (do not know the exact Chinese translation is what, "masking"? )

The original text in a very concise code to clarify the problem

 Public classMain { Public Static voidMain (string[] args)throwsException {(NewOuterclass ()).NewInnerclass (). Printx (2); System.out.println ("Finish!"); }}classOuterclass {intx = 0; classInnerclass {intx = 1;  Public voidPrintx (intx) {System.out.println ("X=" +x); System.out.println ("This.x=" + This. x); System.out.println ("outerclass.this.x=" + outerclass. This. x); }    }}

Output:

x=2this.x=1outerclass.this.x=0finish!

Serialization
    • Serialization of inner classes, including local and annonymous classes, is strongly discouraged.
    • The reason is that when the compiler compile certain constructs , it produces synthetic Constructs:these is classes, methods, fields, and other Constructs that does not has a corresponding construct in the source code.
    • Compatibility issues If you serialize an inner class and then deserialize it with a different JRE Implementat Ion.

Java-nested Classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.