Access non-static internal classes in static methods

Source: Internet
Author: User
Public class Outer {
Public string name = "outer ";
Public static void main (string argv []) {
// Inner myinner = new inner (); // compilation error occurs when you use this statement directly.
Outer myouter = new outer (); // first create an object of the external class
Outer. Inner myinner = myouter. New inner ();
Myinner. showname ();
} // End of main
// The following section CodeUsed to test this n-annoying Approach
Public void amethod (){
Outer myouter = new outer ();
Outer. Inner myinner = myouter. New inner ();
Myinner. showname ();
}
// Use non-static methods to access non-static internal classes
Private class inner {
String name = new string ("inner ");
Void showname (){
System. Out. println (name );
}
} // End of inner class
}
Directly create the object of the internal class in non-static method access non-static internal class: New inner (). showname (); of course, you can also use this n-Way to assume that private class inner is changed to static private class inner, access to the static internal class in the static method is also the object that directly creates the internal class, that is, inner myinner = new inner (), or outer. inner myinner = new outer. inner () can also work. It can be seen that this n-way can be used in the above three cases.

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.