Nested classes: Statically nested classes and non-static nested classes

Source: Internet
Author: User

Internal classes are defined in Wikipedia as: In object-oriented programming, an inner class (also called a nested Class) is a class that is declared in another class or interface. The inner class differs from the subclass (subclass). (Translator Note: the wiki annotations are incorrect, and the inner and nested classes are not identical, see below.) )

In Java, the above definition can be an example of the following:

1  Public Final classClazz {2     Private Final classInnerclazzImplementsRunnable {3          PublicInnerclazz () {4         }           5 @Override6          Public voidrun () {7System.out.println ("Hello World"); 8         }   9     }     Ten      One      PublicClazz () { A     }      -      PublicRunnable getrunnable () { -         return NewInnerclazz ();  the     }  -}

In addition to declaring an inner class, the above example does nothing else. I would like to highlight the difference between nested classes and internal classes, because not all programmers understand this.

Internal classes (InnerClass)

An inner class "Innerclazz" is declared in the example code above. Each time the Getrunnable method is called, an instance of the Innerclazz class is created. If you debug this code in FindBugs, you will see this warning message: SIC: It should be a static inner class (Sic_inner_should_be_static). This class is an inner class created by its external objects, but it does not use built-in references to external objects. This reference can make the inner class instance larger, and can make the external object's lifetime as long as possible. If possible, this class should be defined as static.

This warning message is clear: the inner class retains a reference to its parent class, so as long as the Innerclazz class is referenced, its parent class cannot be automatically garbage collected by the JVM's garbage collection mechanism (the internal class and its parent class are very stable, and you can learn more by using the memory management article). If you want to use this reference, the following nested classes are very useful:

1PublicFinalClassClazz {2PrivateFinalClass InnerclazzImplementsRunnable {3PublicInnerclazz () {4}5@Override6PublicvoidRun () {7//Print the value of a member of its "parent" class8//It ' s possible because the inner class has an implicit reference9//On the Clazz instance10System.out.println (_currentnumber);11}12}  private int _currentnumber = 0; public  clazz () {Runnable} + public    getrunnable () {_currentnumber++
                                                     
                                                      ; 
                                                       return new innerclazz ();          
                                                             +} 

Nested classes of non-intrinsic classes (Nested but not inner class)

If you do not need to preserve a very strong reference relationship between the Clazz instance and the Innerclazz instance, declare Innerclazz as a static member class (The Nestednotinnerclazz class in the following example).

1PublicFinalClassClazz {2//Static keyword is added3StaticPrivateFinalClass NestednotinnerclazzImplementsRunnable {4PublicNestednotinnerclazz () {5}6@Override7PublicvoidRun () { 8 System.out.println ("Hello World"  9  10  11 12 public Clazz () { Span style= "color: #008080;" >13  14 public Runnable getrunnable () {15 return new Nestednotinnerclazz ();  17}       

Nested classes: statically nested classes and non-static nested classes

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.