<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java - 靜態類,塊,方法和變量 > 原文: [https://beginnersbook.com/2013/04/java-static-class-block-methods-variables/](https://beginnersbook.com/2013/04/java-static-class-block-methods-variables/) `static`關鍵字可以與類,變量,方法和塊一起使用。靜態成員屬于類而不是特定實例,這意味著如果您將成員設為靜態,則可以在不使用對象的情況下訪問它。我們舉一個例子來理解這個: 這里我們有一個靜態方法`myMethod()`,我們可以在沒有任何對象的情況下調用這個方法,因為當我們將一個成員靜態化時它就變成了類級別。如果我們刪除`static`關鍵字并使其成為非靜態關鍵字,那么我們必須創建一個類的對象才能調用它。 > 靜態成員對于類的所有實例(對象)是通用的,但非靜態成員對于每個類實例是獨立的。 ```java class SimpleStaticExample { // This is a static method static void myMethod() { System.out.println("myMethod"); } public static void main(String[] args) { /* You can see that we are calling this * method without creating any object. */ myMethod(); } } ``` 輸出: ```java myMethod ``` ## 靜態塊 靜態塊用于初始化靜態變量。當在內存中加載類時,將執行此塊。一個類可以有多個靜態塊,它們將按照它們寫入程序的相同順序執行。 ### 示例 1:單個靜態塊 正如您所看到的,在我們在`main`方法中訪問它們之前,兩個靜態變量都已初始化。 ```java class JavaExample{ static int num; static String mystr; static{ num = 97; mystr = "Static keyword in Java"; } public static void main(String args[]) { System.out.println("Value of num: "+num); System.out.println("Value of mystr: "+mystr); } } ``` 輸出: ```java Value of num: 97 Value of mystr: Static keyword in Java ``` ### 示例 2:多個靜態塊 讓我們看看多個靜態塊如何在 Java 中工作。它們以給定的順序執行,這意味著第一個靜態塊在第二個靜態塊之前執行。這就是原因,第一個塊初始化的值被第二個塊覆蓋。 ```java class JavaExample2{ static int num; static String mystr; //First Static block static{ System.out.println("Static Block 1"); num = 68; mystr = "Block1"; } //Second static block static{ System.out.println("Static Block 2"); num = 98; mystr = "Block2"; } public static void main(String args[]) { System.out.println("Value of num: "+num); System.out.println("Value of mystr: "+mystr); } } ``` **輸出:** ```java Static Block 1 Static Block 2 Value of num: 98 Value of mystr: Block2 ``` ## Java 靜態變量 靜態變量對于類的所有實例(或對象)是通用的,因為它是類級變量。換句話說,您可以說只創建了一個靜態變量副本,并在該類的所有實例之間共享。這些變量的內存分配僅在類加載到內存中時才會發生一次。 幾點重點: * 靜態變量也稱為類變量。 * 與**非靜態變量**不同,這些變量可以直接在靜態和非靜態方法中訪問。 ### 示例 1:可以在靜態方法中直接訪問靜態變量 這里我們有一個靜態方法`disp()`和兩個靜態變量`var1`和`var2`。這兩個變量都可以在靜態方法中直接訪問。 ```java class JavaExample3{ static int var1; static String var2; //This is a Static Method static void disp(){ System.out.println("Var1 is: "+var1); System.out.println("Var2 is: "+var2); } public static void main(String args[]) { disp(); } } ``` **輸出:** ```java Var1 is: 0 Var2 is: null ``` ### 示例 2:靜態變量在類的所有實例之間共享 在此示例中,字符串變量是非靜態的,整數變量是靜態的。正如您在輸出中看到的那樣,兩個對象的非靜態變量是不同的,但靜態變量在它們之間共享,這就是對象`ob2`對靜態變量所做的更改在兩個對象中反映的原因。 ```java class JavaExample{ //Static integer variable static int var1=77; //non-static string variable String var2; public static void main(String args[]) { JavaExample ob1 = new JavaExample(); JavaExample ob2 = new JavaExample(); /* static variables can be accessed directly without * any instances. Just to demonstrate that static variables * are shared, I am accessing them using objects so that * we can check that the changes made to static variables * by one object, reflects when we access them using other * objects */ //Assigning the value to static variable using object ob1 ob1.var1=88; ob1.var2="I'm Object1"; /* This will overwrite the value of var1 because var1 has a single * copy shared among both the objects. */ ob2.var1=99; ob2.var2="I'm Object2"; System.out.println("ob1 integer:"+ob1.var1); System.out.println("ob1 String:"+ob1.var2); System.out.println("ob2 integer:"+ob2.var1); System.out.println("ob2 STring:"+ob2.var2); } } ``` 輸出: ```java ob1 integer:99 ob1 String:I'm Object1 ob2 integer:99 ob2 STring:I'm Object2 ``` 有關參考的更多詳細信息: [Java - 靜態變量](https://beginnersbook.com/2013/05/static-variable/) ## Java 靜態方法 靜態方法可以在不使用類的對象(實例)的情況下訪問類變量(靜態變量),但是只能使用對象訪問非靜態方法和非靜態變量。 可以在靜態和非靜態方法中直接訪問靜態方法。 **語法:** 靜態關鍵字后跟返回類型,后跟方法名稱。 ```java static return_type method_name(); ``` ### 示例 1:靜態方法`main`訪問沒有對象的靜態變量 ```java class JavaExample{ static int i = 10; static String s = "Beginnersbook"; //This is a static method public static void main(String args[]) { System.out.println("i:"+i); System.out.println("s:"+s); } } ``` **輸出:** ```java i:10 s:Beginnersbook ``` ### 示例 2:直接在靜態和非靜態方法中訪問的靜態方法 ```java class JavaExample{ static int i = 100; static String s = "Beginnersbook"; //Static method static void display() { System.out.println("i:"+i); System.out.println("i:"+s); } //non-static method void funcn() { //Static method called in non-static method display(); } //static method public static void main(String args[]) { JavaExample obj = new JavaExample(); //You need to have object to call this non-static method obj.funcn(); //Static method called in another static method display(); } } ``` 輸出: ```java i:100 i:Beginnersbook i:100 i:Beginnersbook ``` 閱讀更多:[靜態方法與 Java 中的非靜態方法](https://beginnersbook.com/2013/05/static-vs-non-static-methods/) ## 靜態類 只有當它是嵌套類時,才能使**靜態**成為類。 1. 嵌套的靜態類不需要引用外類 2. 靜態類無法訪問外部類的非靜態成員 我們將在一個例子的幫助下看到這兩點: ### 靜態類示例 ```java class JavaExample{ private static String str = "BeginnersBook"; //Static class static class MyNestedClass{ //non-static method public void disp() { /* If you make the str variable of outer class * non-static then you will get compilation error * because: a nested static class cannot access non- * static members of the outer class. */ System.out.println(str); } } public static void main(String args[]) { /* To create instance of nested class we didn't need the outer * class instance but for a regular nested class you would need * to create an instance of outer class first */ JavaExample.MyNestedClass obj = new JavaExample.MyNestedClass(); obj.disp(); } } ``` 輸出: ```java BeginnersBook ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看