The object-oriented (where is the inner class) of the first season of getting Started with Java? )

Source: Internet
Author: User

/* Internal class overview: Define classes Inside other classes, which are called inner classes. Example: A class B is defined in Class A, and Class B is an inner class. Internal Access features: A: Inner classes can access members of external classes directly, including private. B: The external class must create an object to access the members of the inner class.                    Inner Class location member location: The class defined in the member location, called the member inner class. Local location: A class defined in a local location, called a local inner class. */

1, members of the internal class:

/* member Inner class: How to access the members of an inner class directly. The external class name. Internal class Name Object name = External Class object. Inner class object; Object calls the outer bounds of the object */class Outer {private int num = 10;class Inner {//inner class public void show () {System.out.println (num);}} Class InnerClassDemo3 {public static void main (string[] args) {//format: external class name. Inner Class Name Object name = Outer Class object. Inner class object; Outer. Inner oi = new Outer (). New Inner (); Oi.show ();}}
For the modifier of the member's inner class, I want to do the analysis in the second quarter. The second season is written in the form of special topics and questions.



2. Local inner class

/* Local inner Class A: You can directly access the members of the external Class B: In a local location, you can create an inner class object and invoke an inner class method through an object to use the local inner class function interview question: What are the considerations for local internal classes accessing local variables? A: Local internal class access local variables must be decorated with final B: Why? Local variables are called as the method is called, and disappear as the call finishes. The contents of the heap memory do not disappear immediately. So, add the final modifier. When the final modifier is added, the variable becomes a constant. Since it is a constant. Disappeared. The data stored in memory is 20, so there is still data in use. */class Outer {private int num  = 10;public void Method () {//int num2 = 20;//is not a final variable, error final int num2 = 20;//Access local variable from inner class num2; Required to be declared as the final final type class Inner {public void Show () {System.out.println (num); System.out.println (num2);//20,20 is a constant, and 20 of this data is used in memory. }}//system.out.println (num2); Inner i = new Inner ();//In a local location, you can create an inner class object, invoke an inner class method through an object, use the local inner class function i.show ();}} Class InnerClassDemo5 {public static void main (string[] args) {Outer o = new Outer (); O.method ();}}



The object-oriented (where is the inner class) of the first season of getting Started with Java? )

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.