當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Static用法及代碼示例


Java 允許在一個類中定義另一個類。這些被稱為嵌套類。大多數開發人員都知道類可以是靜態的,因此在 Java 中可以將某些類設為靜態。 Java支持靜態實例變量,靜態方法,靜態塊和靜態類。定義嵌套類的類稱為外層類。與頂級課程不同,嵌套類可以是靜態的。非靜態嵌套類也稱為內部類.

Note: The top level class cannot be static in java, to create a static class we must create a nested class and then make it static.

如果沒有外部類的實例,則無法創建內部類的實例。因此,內部類實例可以訪問其外部類的所有成員,而無需使用外部類實例的引用。因此,內部類可以幫助使程序變得簡單和簡潔。

Remember: In static class, we can easily create objects.

靜態和非靜態嵌套類的區別

以下是靜態嵌套類和內部類之間的主要區別。

  1. 靜態嵌套類可以在不實例化其外部類的情況下實例化。
  2. 內部類可以訪問外部類的靜態和非靜態成員。靜態類隻能訪問外部類的靜態成員。

示例靜態和非靜態嵌套類

下麵是上麵提到的topic的實現:

Java


// Java program to Demonstrate How to
// Implement Static and Non-static Classes
// Class 1
// Helper class
class OuterClass {
    // Input string
    private static String msg = "GeeksForGeeks";
    // Static nested class
    public static class NestedStaticClass {
        // Only static members of Outer class
        // is directly accessible in nested
        // static class
        public void printMessage()
        {
            // Try making 'message' a non-static
            // variable, there will be compiler error
            System.out.println(
                "Message from nested static class: " + msg);
        }
    }
    // Non-static nested class -
    // also called Inner class
    public class InnerClass {
        // Both static and non-static members
        // of Outer class are accessible in
        // this Inner class
        public void display()
        {
            // Print statement whenever this method is
            // called
            System.out.println(
                "Message from non-static nested class: "
                + msg);
        }
    }
}
// Class 2
// Main class
class GFG {
    // Main driver method
    public static void main(String args[])
    {
        // Creating instance of nested Static class
        // inside main() method
        OuterClass.NestedStaticClass printer
            = new OuterClass.NestedStaticClass();
        // Calling non-static method of nested
        // static class
        printer.printMessage();
        // Note: In order to create instance of Inner class
        //  we need an Outer class instance
        // Creating Outer class instance for creating
        // non-static nested class
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner
            = outer.new InnerClass();
        // Calling non-static method of Inner class
        inner.display();
        // We can also combine above steps in one
        // step to create instance of Inner class
        OuterClass.InnerClass innerObject
            = new OuterClass().new InnerClass();
        // Similarly calling inner class defined method
        innerObject.display();
    }
}
輸出
Message from nested static class: GeeksForGeeks
Message from non-static nested class: GeeksForGeeks
Message from non-static nested class: GeeksForGeeks





相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Static class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。