java hashset,java關鍵字static_Java中的static關鍵字

 2023-11-19 阅读 27 评论 0

摘要:java關鍵字staticstatic keyword in Java is used a lot in java programming. Java static keyword is used to create a Class level variable in java. static variables and methods are part of the class, not the instances of the class. Java中的static關鍵字在Java編

java關鍵字static

static keyword in Java is used a lot in java programming. Java static keyword is used to create a Class level variable in java. static variables and methods are part of the class, not the instances of the class.

Java中的static關鍵字在Java編程中經常使用。 Java static關鍵字用于在Java中創建類級別的變量。 靜態變量和方法是類的一部分,而不是類的實例。

Java中的static關鍵字 (static keyword in java)

Java static keyword can be used in five cases as shown in below image.

如下圖所示,可以在五種情況下使用Java靜態關鍵字。

java hashset、We will discuss four of them here, the fifth one was introduced in Java 8 and that has been discussed at Java 8 interface changes.

我們將在這里討論其中的四個,第五個是在Java 8中引入的,并且已經在Java 8接口更改中進行了討論。

  1. Java靜態變量 (Java static variable)

    We can use static keyword with a class level variable. A static variable is a class variable and doesn’t belong to Object/instance of the class.

    Since static variables are shared across all the instances of Object, they are not thread safe.

    Usually, static variables are used with the final keyword for common resources or constants that can be used by all the objects. If the static variable is not private, we can access it with ClassName.variableName

    //static variable exampleprivate static int count;public static String str;public static final String DB_USER = "myuser";

    我們可以在類級別的變量中使用static關鍵字。 靜態變量是類變量,不屬于該類的對象/實例。

    java static變量。 由于靜態變量在Object的所有實例之間共享,因此它們不是線程安全的 。

    通常,靜態變量與final關鍵字一起使用,以表示所有對象都可以使用的公共資源或常量。 如果靜態變量不是私有變量,則可以使用ClassName.variableName進行訪問

  2. Java靜態方法 (Java static method)

    Same as static variable, static method belong to class and not to class instances.

    A static method can access only static variables of class and invoke only static methods of the class.

    Usually, static methods are utility methods that we want to expose to be used by other classes without the need of creating an instance. For example Collections class.

    Java Wrapper classes and utility classes contains a lot of static methods. The main() method that is the entry point of a java program itself is a static method.

    //static method examplepublic static void setCount(int count) {if(count > 0)StaticExample.count = count;}//static util methodpublic static int addInts(int i, int...js){int sum=i;for(int x : js) sum+=x;return sum;}

    java static,From Java 8 onwards, we can have static methods in interfaces too. For more details please read Java 8 interface changes.

    與靜態變量相同,靜態方法屬于類,而不屬于類實例。

    靜態方法只能訪問類的靜態變量,并且只能調用類的靜態方法。

    通常,靜態方法是我們希望公開的實用程序方法,供其他類使用,而無需創建實例。 例如Collections類 。

    Java包裝程序類和實用程序類包含許多靜態方法。 作為Java程序本身入口點的main()方法是靜態方法。

    從Java 8開始,我們也可以在接口中使用靜態方法。 有關更多詳細信息,請閱讀Java 8接口更改 。

  3. Java靜態塊 (Java static block)

    java構造方法。Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader.

    Static block is used to initialize the static variables of the class. Mostly it’s used to create static resources when the class is loaded.

    We can’t access non-static variables in the static block. We can have multiple static blocks in a class, although it doesn’t make much sense. Static block code is executed only once when the class is loaded into memory.

    static{//can be used to initialize resources when class is loadedSystem.out.println("StaticExample static block");//can access only static variables and methodsstr="Test";setCount(2);}

    Java靜態塊是由Java ClassLoader將類加載到內存中時執行的語句組。

    靜態塊用于初始化類的靜態變量。 通常,它用于在加載類時創建靜態資源。

    我們無法在靜態塊中訪問非靜態變量。 我們可以在一個類中有多個靜態塊,盡管這沒有多大意義。 當類加載到內存中時,靜態塊代碼僅執行一次。

  4. Java靜態類 (Java Static Class)

    java的static關鍵字。We can use static keyword with nested classes. static keyword can’t be used with top-level classes.

    A static nested class is same as any other top-level class and is nested for only packaging convenience.

    Read: Java Nested Classes

    我們可以對嵌套類使用static關鍵字。 static關鍵字不能與頂級類一起使用。

    靜態嵌套類與任何其他頂級類相同,并且僅出于包裝方便而嵌套。

    閱讀: Java嵌套類

static關鍵字的作用。Let’s see all the static keyword in java usage in a sample program.

讓我們在示例程序中查看Java用法中的所有static關鍵字。

StaticExample.java

StaticExample.java

package com.journaldev.misc;public class StaticExample {//static blockstatic{//can be used to initialize resources when class is loadedSystem.out.println("StaticExample static block");//can access only static variables and methodsstr="Test";setCount(2);}//multiple static blocks in same classstatic{System.out.println("StaticExample static block2");}//static variable exampleprivate static int count; //kept private to control its value through setterpublic static String str;public int getCount() {return count;}//static method examplepublic static void setCount(int count) {if(count > 0)StaticExample.count = count;}//static util methodpublic static int addInts(int i, int...js){int sum=i;for(int x : js) sum+=x;return sum;}//static class example - used for packaging convenience onlypublic static class MyStaticClass{public int count;}}

Let’s see how to use static variable, method and static class in a test program.

讓我們看看如何在測試程序中使用靜態變量,方法和靜態類。

static關鍵字的用法、TestStatic.java

TestStatic.java

package com.journaldev.misc;public class TestStatic {public static void main(String[] args) {StaticExample.setCount(5);//non-private static variables can be accessed with class nameStaticExample.str = "abc";StaticExample se = new StaticExample();System.out.println(se.getCount());//class and instance static variables are sameSystem.out.println(StaticExample.str +" is same as "+se.str);System.out.println(StaticExample.str == se.str);//static nested classes are like normal top-level classesStaticExample.MyStaticClass myStaticClass = new StaticExample.MyStaticClass();myStaticClass.count=10;StaticExample.MyStaticClass myStaticClass1 = new StaticExample.MyStaticClass();myStaticClass1.count=20;System.out.println(myStaticClass.count);System.out.println(myStaticClass1.count);}}

The output of the above static keyword in java example program is:

在Java示例程序中,上述static關鍵字的輸出為:

StaticExample static block
StaticExample static block2
5
abc is same as abc
true
10
20

Notice that static block code is executed first and only once as soon as class is loaded into memory. Other outputs are self-explanatory.

請注意,靜態塊代碼首先執行,并且僅在類加載到內存后才執行一次。 其他輸出是不言自明的。

Java靜態導入 (Java static import)

java static類,Normally we access static members using Class reference, from Java 1.5 we can use java static import to avoid class reference. Below is a simple example of Java static import.

通常,我們使用類引用訪問靜態成員,從Java 1.5開始,我們可以使用Java靜態導入來避免類引用。 以下是Java靜態導入的簡單示例。

package com.journaldev.test;public class A {public static int MAX = 1000;public static void foo(){System.out.println("foo static method");}
}
package com.journaldev.test;import static com.journaldev.test.A.MAX;
import static com.journaldev.test.A.foo;public class B {public static void main(String args[]){System.out.println(MAX); //normally A.MAXfoo(); // normally A.foo()}
}

Notice the import statements, for static import we have to use import static followed by the fully classified static member of a class. For importing all the static members of a class, we can use * as in import static com.journaldev.test.A.*;. We should use it only when we are using the static variable of a class multiple times, it’s not good for readability.

注意import語句,對于靜態導入,我們必須使用import static然后再使用類的完全分類的static成員。 為了導入一個類的所有靜態成員,我們可以像在import static com.journaldev.test.A.*;那樣使用import static com.journaldev.test.A.*; 。 僅當我們多次使用類的靜態變量時,才應使用它,這不利于可讀性。

Update: I have recently created a video to explain static keyword in java, you should watch it below.

更新 :我最近創建了一個視頻來解釋Java中的static關鍵字,您應該在下面觀看。

c++ static關鍵字的作用、演示地址

翻譯自: https://www.journaldev.com/1365/static-keyword-in-java

java關鍵字static

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/182885.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息