Static inner classes in Java

Source: Internet
Author: User

static inner classes are internal classes of static adornments

Characteristics of Static internal classes :

1. Static inner classes cannot directly access non-static members of an external class, but can be accessed through the new external Class (). member ;

2. If a static member in an external class is the same as a member name in a static inner class, you can access the static members of the external class by using the class name.

If the static members in the outer class and the member names in the static inner class are different, the static members of the external class can be accessed directly from the static member name ;

3. Create an object of the static inner class without requiring an object of the outer class, directly with the static Inner class object name = new static inner class ();

1  Packagecom.imooc.test003;2 3  Public classOuter {4 5     Private intA = 10;//ordinary member variables for external classes6     Private Static intb = 20;//static member variables for external classes7     Private Static intc = 30;//static member variables for external classes8     9     //Inner class innerTen      Public Static classinner{ One          A         Private intB = 40; -          -          Public voidTest () { theSYSTEM.OUT.PRINTLN ("Accessing ordinary member variables in an external Class A:" +NewOuter (). a); -SYSTEM.OUT.PRINTLN ("Access static member variable B with the same name as the member variable in the inner class in the external class:" +outer.b); -SYSTEM.OUT.PRINTLN ("Accessing ordinary member variable B in an inner class:" +b); -System.out.println ("A static member variable that accesses a member variable in an external class with a different name in the inner Class C:" +c); +         } -          +     } A      at      Public Static voidMain (string[] args) { -Inner in =NewInner ();//to create an object of an inner class - in.test (); -     } -      -}
View Code

Reference: MU-Class network (http://www.imooc.com/)

Static 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.