Teach you to use Java static inner classes

Source: Internet
Author: User

12.3.4 static Inner class

In the previous section we discussed the inner class, which includes one or more classes in a class (see 12.3.3 section of this book). Like an inner class, a static inner class means that there is another or more static class inside a class. For example:

public class OuterClass {
    ...
    static class StaticInnerClass1 {             //内部静态类
        //只可以访问OuterClass的静态成员
         ...
    }                                            //StaticInnerClass结束
    ...                             
    static class StaticInnerClassN {            // 更多静态内部类
       //只可以访问OuterClass的静态成员
       ...
    }                                           //StaticInnerClassN结束
}  //OuterClass结束

Unlike general internal classes, the This action is not available in static code, so only static variables and static methods of external classes can be accessed in static inner classes. The purpose of using a static inner class is the same as using an inner class. If an inner class does not depend on an instance variable of its outer class, or is independent of the instance variable, select Apply the static inner class.

The following example shows how to use a static inner class:

Full program exists this book supporting resources directory Ch12 named Staticinnerclasstest.java
public class Staticinnerclasstest {
public static void Main (String args[]) {
OUTERCLASS2 outer = new OuterClass2 ();
OuterClass2.StaticInnerClass.innerMethod ();
Calling static methods of static inner classes
Outerclass2.outermethod ();
To create a static inner class object
Outerclass2.staticinnerclass Staticinner = new Outerclass2.staticinnerclass ();
int num = STATICINNER.INNERMETHOD2 (); Calling static internal class instance methods
}
}
Class OuterClass2 {//External classes
Private double x = 0.0; An internal static class cannot access an external class instance variable
static private int n = 10; External class static variables
static void Outermethod () {//External class Statics method
System.out.println ("from Outerclass ...");
}
void OuterMethod2 () {
System.out.println ("From Outerclass ' instance method2 () ...");
}
Static class Staticinnerclass {//static inner class
static private int m = 5; Static internal class static variables
static void Innermethod () {//Static internal class Statics method
int sum;
n = 20; Only external class static variables can be accessed
sum = n + M;
System.out.println ("from innerclass sum =" + sum);
Outermethod (); Only external class static methods can be invoked
}
int InnerMethod2 () {
n = 100;
Outermethod ();
System.out.println ("from InnerMethod2 () n =" + N);
return n;
}
}//Static inner class end
}//External class end

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.