Internal class learning (5)-static internal class (static inner class)

Source: Internet
Author: User

Here we will introduce the last special internal class-static inner class, which adds the static modifier (modifier) before the internal class ). Note: Only the internal class can be declared as the static type. Generally, we cannot use static when declaring a common class. Otherwise, the compilation fails.

So why should we use static internal classes? Under what circumstances should we use static internal classes? As we have mentioned earlier, the compiler will automatically add a reference to the internal class to point to the object of the external class that generates it. If you do not want or need this reference, then we can declare this internal class as static to prohibit the generation of this reference. In addition, the use of static classes is the same as that of common static classes. Example:

Package cn.edu. HUST. cm. test;

Public class staticinnerclasstest {

Public staticinnerclasstest (){

Super ();

// Todo auto-generated constructor stub

}

 

Public static void main (string [] ARGs ){

// Todo auto-generated method stub

Double D [] = new double [20];

For (INT I = 0; I <D. length; I ++)

D [I] = 100 * Math. Random ();

// Findminmax test = new findminmax ();

Findminmax. Pair pair = findminmax. getminmax (d );

System. Out. println ("minimum value:" + pair. getfirst ());

System. Out. println ("maximum value:" + pair. getsecond ());

}

 

}

 

Class findminmax {

Static double min = double. max_value;

Static double max = double. min_value;

Public static pair getminmax (double d []) {

For (double value: d ){

If (min> value) min = value;

If (max <value) max = value;

}

Return new pair (Min, max );

}

Public static class pair {

Public pair (double first, double second ){

This. First = first;

This. Second = second;

}

Public double getfirst (){

Return this. First;

}

Public double getsecond (){

Return this. Second;

}

Private double first;

Private double second;

}

}

In this example, static internal classes are used mainly because the getminmax method is static and called directly by the class. The syntax for creating an internal class is as follows:

Outerclassobject. New innerclassname (). If outerclassobject is omitted, it is

This. new innerclassname (), outerclassobject, or this refers to an external class object that creates this internal class object. It is an implicit parameter that will be passed in to the internal class Constructor (as described above ). However, this internal class object is directly created by the "class" and does not generate such an implicit parameter to pass in the internal class constructor, therefore, the internal class does not need to "the compiler automatically adds a reference to the internal class to point to the object of the external class that generates it", so we declare this internal class as static. In the above Code, if we remove static, an error will be reported, unless we remove the static of getminmax and call this method through an instance of findminmax, as shown below:

Findminmax test = new findminmax ();

Test. getminmax (d );

Let's take a look at the class file reflection (reflection) generated by the internal Department class. The result is as follows:

Class cn.edu. HUST. cm. Test. findminmax $ pair

{

Public cn.edu. HUST. cm. Test. findminmax $ pair (double, double );

Public double getsecond ();

Public double getfirst ();

Private double first;

Private double second;

}

Clearly, the compiler does not automatically add a reference to the external class that generates it, nor add a findminmax parameter to the constructor.

 

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.