Those things in the Java inner class and the outer class

Source: Internet
Author: User

Yesterday, when the written test encountered the creation of the internal class of Java and access to the problem, I do not understand, did not write, so today up deliberately to test, there is this summary of the article.

Inner classes in Java are also classified as non-static inner classes (anonymous inner classes are also non-static inner classes) and static inner classes, which are different from the external classes and are created differently.

1 Non-static inner class

When constructing a non-static inner class of Java, a reference to an external class is passed in, and as an attribute of an inner class, the inner class implicitly holds a reference to its outer class. In other words, a non-static inner class needs an instance of an external class to be constructed when it is constructed, and it cannot be generated directly from new, as in the case of a normal Java class; A simple example is as follows:

1 Importjava.util.ArrayList;2 Importjava.util.LinkedList;3 Importjava.util.List;4 ImportJava.util.Queue;5 ImportJava.util.Scanner;6 7  Public classMain {8 9     /**Ten      * @paramargs One      */ A      Public  intK=3; -     PrivateStatic String string= "Java"; -     protected floatj=1.5f; the      PublicStaticvoidShow () { -System.out.println ("Show"); -     } -     Private voidAdd () { +System.out.println ("Add"); -     } +      Public Static voidMain (string[] args) { A         //TODO auto-generated Method Stub atMain m=NewMain (); -         //How to construct a legal non-static inner class -Child C=m.NewChild (); -         //Child c=new Child () It's illegal to construct a method - c.test (); -   in     } - //Inner class child to   classchild{ +       Public inti; -       Public voidTest () { theSystem.out.println ("k=:" +k); *System.out.println ("string:" +string); $ Add ();Panax NotoginsengSystem.out.println ("j=:" +j); - Show (); the      } +       A  } the  +}

and non-static inner classes can access all member variables and methods of the outer class, including static member variables and methods, and execute the inner class child's test () method to get the following result:

1 k=:32string:java3add4 j=:1.55 Show
2 static inner class

Static inner classes in Java do not require references to external classes when constructed, so static inner classes do not hold references to external classes, and static inner classes can access only static member variables and methods of external classes. A simple example is (make some simple changes to the code above):

1  Public classMain {2 3     /**4      * @paramargs5      */6      Public  intK=3;7     Private StaticString string= "Java";8     protected floatj=1.5f;9      Public Static voidShow () {TenSystem.out.println ("Show"); One     } A     Private voidAdd () { -System.out.println ("Add"); -     } the      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub -Main m=NewMain (); -         //Child C=m.new Childe ();//illegal +Child c=NewChild (); - c.test (); +   A     } at   Static classchild{ -       Public inti; -      -       Public voidTest () { - //System.out.println ("k=:" +k);//cannot access external class non-static variables -System.out.println ("string:" +string); in          //Add ();//non-static methods that cannot access external classes - //System.out.println ("j=:" +j); to Show (); +      } -       the  } *  $}

As you can see from the code above, the static inner class is created in the same way that a normal Java class is created, and executing the 21st line of code gives you the following result:

1 String:java 2 Show
3 Talk

This creation of a non-static inner class of Java implicitly holds a reference to an external class, and by default this reference is a strong reference, so if the inner class's life cycle is longer than the life cycle of the outer class, the program can easily generate a memory leak (you think the garbage collector reclaims instances of external classes, However, because the inner class holds references to external classes, the garbage collector does not work correctly. To avoid this, you can display a soft reference (or weak reference) that holds an external class inside the inner class, and pass it through in the form of a constructor, to determine whether the outer class is being recycled in the process of using the inner class.

For this point of memory leaks, there are two articles in the Black House of Reference technology:memory leaks caused by handler in Android and avoidance of memory leaks caused by context in Android

Those things in the Java inner class and the outer 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.