Java (inner Class)

Source: Internet
Author: User
Tags class definition

Inner class:

A class definition is called an inner class within another class.

Categories of inner classes:

1. member Inner class:
2. Local Inner class:

1. member Inner class:

Access to members ' internal classes:

    方式一:在成员内部类的外侧提供一个方法创建内部类的对象进行访问,其它类使用时,创建该成员内部类的外部类的对象,由外部对象调用在外部类外侧的方法。    方式二:在其他类直接创建内部类的对象。           格式:外部类.内部类  new 外部类().new 内部类();

Note: If it is a static inner class, then the format created in the other class:

new 外部类.内部类();

The application scenario of the inner class: When we describe a thing, we find that there is another complex thing in the inside of a thing, B, and this more complex thing B also needs to access data such as the properties of a thing, then we can use the inner class to describe the B thing.

//External classclassouter{//Member variables    intx = -;The ////Outer.class file is loaded into memory when it is present in memory. Static member data is not required for object presence to be accessible.     //Member inner class    Static  classinner{Static  inti =Ten; Public void Print() {System. out.println("This is the print method of the member's inner Class!" "+i); }    }//Creates an object of the inner class in an external method, and then calls the internal method.      Public void instance() {Inner Inner =New Inner(); Inner.Print(); }}//Other classesclassDemo4 { Public Static void Main(string[] args) {System. out.println(Outer.Inner.I); Outer Outer =New Outer();//The first method of AccessOuter.instance(); Outer.InnerInner =New Outer().New Inner();//second method of AccessInner.Print(); Outer.InnerInner =NewOuter.Inner(); Inner.Print(); }}

Benefits of inner classes: The inner class can access all members of the outer class directly.
Internal classes to note the details:

1. If the external class has a member variable with the same name as the inner class, the member variable of the inner class is accessed by default in the inner class. You can specify access to members of external classes through the external class. this. member variable name.
2. A private member inner class can only be accessed in an external class that provides a method for creating an object of an inner class, and cannot create an object in another class.
3. member inner class once a static member appears, the class must also use the static modifier.

Anonymous inner class: A class without a class name is called an anonymous inner class.

The benefits of anonymous inner classes: simplify writing.

The use of anonymous inner classes is premised on the existence of an inheritance or implementation relationship before it can be used.

Anonymous inner classes are generally used for arguments.

2. Local Inner class:

Another class is defined inside a method of a class, and another class is called a local inner class.

Local inner classes to note the details:
If the local inner class accesses a local variable, the local variable must use the final decoration.

classouter{String name="Name of the external class"; Public void Test(){//Local variables        Final inty = -;when did the//y disappear from memory? Y disappears after the method finishes executing.         //local inner class        classinner{/*when the test method finishes executing, then y disappears from memory immediately, and the inner object is in the methodthe execution has not disappeared from memory, and the print method of the inner object is still accessingY variable, when the y variable is gone, it gives the impression that Y's life variable has been extended.Solution: If a local inner class accesses a local variable, then let the local inner classaccess the replica of this local variable. */            intx =Ten; Public void Print() {System. out.println("This is the print method for the local inner class ..."+y); }} Inner Inner =New Inner();//When does this inner object disappear? The life cycle of the inner object is longer than the life cycle of the local variable Y. Inner.Print(); }}classDemo5 { Public Static void Main(string[] args) {Outer Outer =New Outer(); Outer.Test(); }}

Java (inner Class)

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.