Android (Java) Learning Note 112: Introduction to the internal classes of local locations

Source: Internet
Author: User

1. Local inner class

1 /*2 local inner class3 A: Members of external classes can be accessed directly4 B: In a local location, you can create an inner class object that invokes an inner class method through an object to use the local inner class functionality5     6 Interview questions:7 local internal class access to local variables considerations?8 A: Local internal class access local variables must be final decorated 9 B: Why?Ten a local variable is called as the method is called, and disappears as the call finishes.  One the contents of the heap memory do not disappear immediately. So, we add final retouching.  A when the final modifier is added, the variable becomes a constant. Since it is a constant. You are gone.  - I'm storing data 20 in memory, so I still have the data in use.  - */ the classOuter { -     Private intnum = 10; -      -      Public voidmethod () { +         //int num2 =; -         //final int num2 =; +         classInner {//If num2 is not final, the local variable disappears after the call to method () is completed, but the new Inner in heap memory does not disappear immediately A              Public voidShow () {///and the object New Inner () is still using num2, but as mentioned earlier num2 has disappeared prematurely, so it will go wrong, add final  at System.out.println (num); -                 //Access local variable num2 from inner class; need to be declared as final type -System.out.println (NUM2);//after---decompile you will see that the final effect is System.out.println (20), replacing the num2 directly with -             } -         } -          in         //System.out.println (num2); -          toInner i =NewInner (); + i.show (); -     } the } *  $ classInnerClassDemo5 {Panax Notoginseng      Public Static voidMain (string[] args) { -Outer o =NewOuter (); the O.method (); +     } A}

Android (Java) Learning Note 112: Introduction to the internal classes of local locations

Related Article

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.