更新時間:2022-12-05 12:37:11 來源:動力節點 瀏覽1112次
Java中的static關鍵字主要用于內存管理。Java中的static關鍵字用于共享給定類的相同變量或方法。用戶可以將靜態關鍵字應用于變量、方法、塊和嵌套類。static 關鍵字屬于類而不是類的實例。static 關鍵字用于常量變量或對類的每個實例都相同的方法。
static關鍵字是 Java 中的非訪問修飾符,適用于以下情況:
積木
變量
方法
班級
注意:要創建靜態成員(塊、變量、方法、嵌套類),請在其聲明之前加上關鍵字static。
當成員聲明為靜態時,可以在創建其類的任何對象之前訪問它,而無需引用任何對象。例如,在下面的 java 程序中,我們在不創建Test類 的任何對象的情況下訪問靜態方法m1() 。
// Java program to demonstrate that a static member
// can be accessed before instantiating a class
class Test
{
// static method
static void m1()
{
System.out.println("from m1");
}
public static void main(String[] args)
{
// calling m1 without creating
// any object of class Test
m1();
}
}
輸出
從 m1
如果您需要進行計算以初始化靜態變量,則可以聲明一個靜態塊,該塊在首次加載類時只執行一次。
考慮以下演示靜態塊使用的 java 程序。
// Java program to demonstrate use of static blocks
class Test
{
// static variable
static int a = 10;
static int b;
// static block
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String[] args)
{
System.out.println("from main");
System.out.println("Value of a : "+a);
System.out.println("Value of b : "+b);
}
}
輸出
靜態塊已初始化。
從主
a 的值:10
b 的值:40
當變量被聲明為靜態時,將創建該變量的單個副本并在類級別的所有對象之間共享。靜態變量本質上是全局變量。該類的所有實例共享同一個靜態變量。
靜態變量的要點:
我們只能在類級別創建靜態變量。
靜態塊和靜態變量按照它們在程序中出現的順序執行。
下面的 Java 程序演示了靜態塊和靜態變量是按照它們在程序中出現的順序執行的。
// Java program to demonstrate execution
// of static blocks and variables
class Test
{
// static variable
static int a = m1();
// static block
static {
System.out.println("Inside static block");
}
// static method
static int m1() {
System.out.println("from m1");
return 20;
}
// static method(main !!)
public static void main(String[] args)
{
System.out.println("Value of a : "+a);
System.out.println("from main");
}
}
輸出
從 m1
內部靜態塊
a 的值:20
從主
當使用static關鍵字聲明方法時,它被稱為靜態方法。靜態方法最常見的例子是main()方法。如上所述,可以在創建其類的任何對象之前訪問任何靜態成員,而無需引用任何對象。聲明為靜態的方法有幾個限制:
他們只能直接調用其他靜態方法。
他們只能直接訪問靜態數據。
他們不能以任何方式引用this或super。
下面是演示靜態方法限制的java程序。
// Java program to demonstrate restriction on static methods
class Test
{
// static variable
static int a = 10;
// instance variable
int b = 20;
// static method
static void m1()
{
a = 20;
System.out.println("from m1");
// Cannot make a static reference to the non-static field b
b = 10; // compilation error
// Cannot make a static reference to the
// non-static method m2() from the type Test
m2(); // compilation error
// Cannot use super in a static context
System.out.println(super.a); // compiler error
}
// instance method
void m2()
{
System.out.println("from m2");
}
public static void main(String[] args)
{
// main method
}
}
輸出
prog.java:18: 錯誤:無法從靜態上下文中引用非靜態變量 b
b = 10; // 編譯錯誤
^
prog.java:22: 錯誤:無法從靜態上下文中引用非靜態方法 m2()
m2(); // 編譯錯誤
^
prog.java:25: 錯誤:無法從靜態上下文中引用非靜態變量 super
System.out.println(super.a); // 編譯器錯誤 ^
prog.java:25: 錯誤:找不到符號
System.out.println(super.a); // 編譯器錯誤 ^
符號:變量a
4個錯誤
對所有對象共有的屬性使用靜態變量。例如,在 Student 類中,所有學生都使用相同的大學名稱。使用靜態方法來更改靜態變量。
考慮以下 java 程序,它說明了靜態關鍵字與變量和方法的用法。
// A java program to demonstrate use of
// static keyword with methods and variables
// Student class
class Student {
String name;
int rollNo;
// static variable
static String cllgName;
// static counter to set unique roll no
static int counter = 0;
public Student(String name)
{
this.name = name;
this.rollNo = setRollNo();
}
// getting unique rollNo
// through static variable(counter)
static int setRollNo()
{
counter++;
return counter;
}
// static method
static void setCllg(String name) { cllgName = name; }
// instance method
void getStudentInfo()
{
System.out.println("name : " + this.name);
System.out.println("rollNo : " + this.rollNo);
// accessing static variable
System.out.println("cllgName : " + cllgName);
}
}
// Driver class
public class StaticDemo {
public static void main(String[] args)
{
// calling static method
// without instantiating Student class
Student.setCllg("XYZ");
Student s1 = new Student("Alice");
Student s2 = new Student("Bob");
s1.getStudentInfo();
s2.getStudentInfo();
}
}
輸出
姓名:愛麗絲
卷號:1
clg名稱:XYZ
姓名:鮑勃
卷號:2
clg名稱:XYZ
以上就是關于“Java中static關鍵字的作用”介紹,大家如果對此比較感興趣,想了解更多相關知識,不妨來關注一下本站的Java教程,里面還有更豐富的知識等著大家去學習,相信對大家一定會有所幫助的。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習