Java Internal classes

Source: Internet
Author: User

Simple Introduction

An inner class is a class that is redefined inside an outer class. An inner class exists as a member of an external class and is attached to an external class. Inner classes can be static, available with protected and private adornments (while external classes can only use public and default package access). The inner classes are mainly of the following categories:

1, into member Internal class

2, local internal class

3 , Static State Internal class

4 , anonymous internal class

Example:

 PackageCOM.WBG; Public classInnerDemo2 { Public Static voidMain (string[] args) {//the instantiation method oneBox box=NewBox (); Box.height=286; Box.inbox InBox=box.NewInBox ();                Inbox.show (); //method of instantiation twoBox.inbox box2=NewBox ().NewInBox ();    Box2.show (); }}//External Classclassbox{//member variables for external classes     Public intheight=198; //member Inner class    classinbox{//members of inner classes         Public voidShow () {System.out.println ("Height of the outer class:" +height); }    }}

Operation Result:

Commonality of inner classes

(1), the inner class is still a separate class, after compiling the inner class is compiled into a separate. class file, but preceded by the class name and the $ symbol of the outer class.

(2), internal classes can not be accessed in an ordinary way.

(3), the inner class is declared static, you cannot casually access the member variables of the outer class, at which point the inner class can only access static member variables of the outer class.

(4), the outer class cannot directly access the members of the inner class, but can be accessed through the Inner class object

An inner class is a member of an external class, so the inner class is free to access the member variables of the outer class, whether private or not.

Because when an object of a perimeter class creates an object of an inner class, this inner class captures an implicit reference that references the enclosing class object that instantiates the inner object. With this pointer, you can access the full state of the perimeter class object.

By deserializing the bytecode of the inner class, the analysis is done mainly by following these steps:
1 The compiler automatically adds a member variable to the inner class that has the same type as the outer class, which is a reference to the Outer class object;
2 The compiler automatically adds a parameter to the construction method of the inner class, which is the type of the outer class, and is used within the constructor method to assign a value to the member variable added in 1;
3 the reference to the external class is passed in by default when the constructor of the inner class is called to initialize the Inner class object.

Static inner class (nested Class)

If you don't need a connection between an inner class object and its enclosing class object, you can declare the inner class as static. This is often referred to as nested classes (nested Class). To understand the meaning of static when applied to inner classes, you must remember that a normal inner class object implicitly holds a reference to the outer class object that created it. However, this is not the case when the inner class is static. A nested class means:

1. To create an object of a nested class, the object of its enclosing class is not required.

2. Non-static perimeter class objects cannot be accessed from objects in the nested class.

Great God (elder brother): Https://www.cnblogs.com/best/p/6736771.html#_lab2_5_0

Java Internal 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.