Java static internal class

Source: Internet
Author: User

If you do not need to associate an internal class object with its peripheral class object, you can declare the internal class as static. This is usually called a nested class ). To understand the meaning of static when applied to an internal class, you must remember that a reference is implicitly saved for a common internal class object, pointing to the peripheral Class Object for creating it. However, this is not the case when the internal class is static. Nested class means: 1. To create an object of the nested class, you do not need the object of its peripheral class. 2. Non-static peripheral class objects cannot be accessed from nested class objects.

Public class Outer {private static int I = 1; private int j = 10; public static void outer_f1 () {} public void outer_f2 () {} // static internal classes can be modified using public, protected, and private. // static internal classes can be defined as static or non-static members. static class Inner {static int inner_ I = 100; int inner_j = 200; static void inner_f1 () {// The static internal class can only access static members of the external class (including static variables and static methods) System. out. println ("Outer. I "+ I); outer_f1 ();} void inner_f2 () {// The static internal class cannot access non-static members of the external class (including non-static variables and non-static methods) // System. out. println ("Outer. I "+ j); // outer_f2 () ;}} public void outer_f3 () {// static members of the internal class accessed by the external class: internal class. static member System. out. println (Inner. inner_ I); Inner. inner_f1 (); // non-static members of the internal class accessed by the external class: instantiate the internal class to Inner inner = new Inner (); inner. inner_f2 ();} public static void main (String [] args) {newOuter (). outer_f3 ();}}

 

Generating a static internal class does not require external class members: this is the difference between the static internal class and the member internal class. Objects of static internal classes can be directly generated: Outer. Inner in = new Outer. Inner (), instead of generating external class objects. In this way, the static internal class is actually a top-level class (normally, you cannot place any code inside the interface, but the nested class can be part of the interface because it is static. Only place the nested class in the interface namespace, which does not violate the interface rules)

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.