Inner classes in Java

Source: Internet
Author: User

Sometimes we need to quickly access some properties of another class in one class, which can be used in the inner class.

classouter{PrivateString str1; PrivateString str2;  PublicOuter (String str1, String str2) { This. str1 =str1;  This. str2 =str2; }         Public classInner { PublicString generatenewstring () {returnSTR1 +" "+str2; }    }     Public voidPrintinfo () {String newstr=NewInner (). generatenewstring (); System. out. println (NEWSTR); } }  Public classhello{ Public Static voidMain (string[] args) {NewOuter ("Hello"," World"). Printinfo (); } }

What if we don't use the inner class to do that? Sure, we just need to make some big changes to the code.

classouter{PrivateString str1; PrivateString str2;  PublicOuter (String str1, String str2) { This. str1 =str1;  This. str2 =str2; }     PublicString getStr1 () {return  This. str1; }     PublicString getStr2 () {return  This. str2; }     Public voidPrintinfo () {String newstr=NewInner (). Generatenewstring ( This); System. out. println (NEWSTR); } } classinner{ PublicString generatenewstring (Outer Outer) {returnOUTER.GETSTR1 () +" "+outer.getstr2 (); }} Public classhello{ Public Static voidMain (string[] args) {NewOuter ("Hello"," World"). Printinfo (); } }

As you can see, after we remove the inner class to the outer class, we must add two getter methods to the outer class for the purposes of the program, and pass an instance of the outer class as a parameter to the inner class. This increases the amount of code in the program, the program structure is also more chaotic.

Now go back to the first piece of code, what if we want to declare and instantiate an instance of inner in the main function?

The format is as follows: Outer.Inner Inner = Outer object. New Inner ();

classouter{PrivateString str1; PrivateString str2;  PublicOuter (String str1, String str2) { This. str1 =str1;  This. str2 =str2; }         Public classInner { PublicString generatenewstring () {returnSTR1 +" "+str2; }    }     Public voidPrintinfo () {String newstr=NewInner (). generatenewstring (); System. out. println (NEWSTR); } }  Public classhello{ Public Static voidMain (string[] args) {Outer.Inner Inner=NewOuter ("Hello"," China").NewInner (); String newstring=inner.generatenewstring (); System. out. println (newstring); } }

Above code output: Hello China

It can be speculated that the inner class is actually a property of the outer. To declare and instantiate a inner class, you must have an instance of the outer class.

But if we don't want to declare the inner class in this strange way, is there a way? The answer is yes. You can declare the inner class to be static.

classouter{Private StaticString str1 ="Hello"; Private StaticString str2 ="America";  PublicOuter (String str1, String str2) { This. str1 =str1;  This. str2 =str2; }         Public Static classInner { PublicString generatenewstring () {returnSTR1 +" "+str2; }    }     Public voidPrintinfo () {String newstr=NewInner (). generatenewstring (); System. out. println (NEWSTR); } }  Public classhello{ Public Static voidMain (string[] args) {Outer.Inner Inner=NewOuter.Inner (); System. out. println (Inner.generatenewstring ()); } }

Above code output: Hello America

Notice that the inner set to STATIC,STR1 and str2 are also static types.

Inner classes in Java

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.