Java Inner class: local inner Class (iii)

Source: Internet
Author: User
Tags java decompiler

The Java inner class is divided into 4 sections, namely, overview, member inner class, local inner class, and anonymous inner class.


In this article is the local inner class of the Java inner class, mainly about the concept of local inner class and in the use of local internal process, need to pay attention to a detail.



1, the concept of local inner class


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



Class Outterclass{void Test () {Class innerclass//local inner class {}}}

In the preceding code, Innerclass is defined inside the Outterclass test method, so Innerclass is a local inner class.



2, local internal class to pay attention to a detail

If a local inner class accesses a local variable, the local variable must use the final decoration


Example code:

Package Com.rk.innerclass;class outterclass{void Test () {int i = 100;final int x = i;//local variable class innerclass//local inner class {void PRI NT () {System.out.println ("The value of X in the" test method is: "+ x);}} Innerclass inner = new Innerclass (); Inner.print (); System.out.println ("i=" + i + ", x=" + x);}} public class Innerclasslearn{public static void Main (string[] args) {Outterclass outter = new Outterclass (); Outter.test () ;}}

In the preceding code, an x variable (final int x = 100) is defined in the test method of the Outterclass class, and the x variable needs to be final decorated because of the need to access the X variable in the local inner class innerclass.


Explain further why X needs to be modified by final:

(1) When does the x variable disappear from memory? When the test method finishes executing, X disappears.

(2) When the test method is finished, then x disappears from memory immediately, and the Innerclass object disappears from memory while the method finishes executing, while the Innerclass object's Print method accesses the x variable, and the x variable is gone. Then it gives the impression that the life variables of x have been extended.


In other words, if the Outterclass class's test method needs to be executed for 5 seconds, the Innerclass class's print method needs to be executed for 10 seconds. After 5 seconds, thetest method of the ① Outterclass class is executed, and the local variable x disappears from the stack memory, while the ②innerclass class's print method continues to execute, and the variable x is also accessed, resulting in a conflict between ① and ②.


Solution: If a "local inner class" accesses a "local variable", then let "this local inner class" Access the copy of the "local variable".

Compile with MyEclipse's 1.4 version of Java compiler and then decompile it with Jd.exe (Java decompiler) to get the following code:

class outterclass{  class 1$innerclass//the "local inner class" into "member inner class"   {     private final int val$x;//here defines a final modified val$x variable          1$innerclass (Int paramint)//Create a parametric construction method     {       this.val$x = paramInt;    }         void print ()     {       System.out.println (the value of x in the "test method is:"  + this.val$x);//This is using its own val$x variable     }   }    void test ()   {    int i = 100 ;    int x = i;    1.innerclass inner =  New 1.innerclass (x);//call 1 here. The constructor of the Innerclass class     inner.print ();         System.out.println ("I= " + i + ", x= " + x);   }} 

Compile with MyEclipse's 1.5 version of Java compiler and then decompile it with Jd.exe (Java decompiler) to get the following code:

Class outterclass{void Test () {int i = 100;        final int x = i;      Object inner = new Object ()//is used here anonymous inner class {void print () {System.out.println ("The value of X in the" test method is: "+ x);    }    };        Inner.print ();  System.out.println ("i=" + i + ", x=" + x); }}



3. Mind Mapping

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/7F/04/wKiom1cPpZrDAjsfAAC1lMLBRUI053.png "title=" Java internal class. png "alt=" Wkiom1cppzrdajsfaac1lmlbrui053.png "/>



Java Inner class: local inner Class (iii)

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.