更新時(shí)間:2022-12-05 12:37:11 來源:動(dòng)力節(jié)點(diǎn) 瀏覽1185次
Java中的static關(guān)鍵字主要用于內(nèi)存管理。Java中的static關(guān)鍵字用于共享給定類的相同變量或方法。用戶可以將靜態(tài)關(guān)鍵字應(yīng)用于變量、方法、塊和嵌套類。static 關(guān)鍵字屬于類而不是類的實(shí)例。static 關(guān)鍵字用于常量變量或?qū)︻惖拿總€(gè)實(shí)例都相同的方法。
static關(guān)鍵字是 Java 中的非訪問修飾符,適用于以下情況:
積木
變量
方法
班級(jí)
注意:要?jiǎng)?chuàng)建靜態(tài)成員(塊、變量、方法、嵌套類),請(qǐng)?jiān)谄渎暶髦凹由详P(guān)鍵字static。
當(dāng)成員聲明為靜態(tài)時(shí),可以在創(chuàng)建其類的任何對(duì)象之前訪問它,而無需引用任何對(duì)象。例如,在下面的 java 程序中,我們?cè)诓粍?chuàng)建Test類 的任何對(duì)象的情況下訪問靜態(tài)方法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
如果您需要進(jìn)行計(jì)算以初始化靜態(tài)變量,則可以聲明一個(gè)靜態(tài)塊,該塊在首次加載類時(shí)只執(zhí)行一次。
考慮以下演示靜態(tài)塊使用的 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);
}
}
輸出
靜態(tài)塊已初始化。
從主
a 的值:10
b 的值:40
當(dāng)變量被聲明為靜態(tài)時(shí),將創(chuàng)建該變量的單個(gè)副本并在類級(jí)別的所有對(duì)象之間共享。靜態(tài)變量本質(zhì)上是全局變量。該類的所有實(shí)例共享同一個(gè)靜態(tài)變量。
靜態(tài)變量的要點(diǎn):
我們只能在類級(jí)別創(chuàng)建靜態(tài)變量。
靜態(tài)塊和靜態(tài)變量按照它們?cè)诔绦蛑谐霈F(xiàn)的順序執(zhí)行。
下面的 Java 程序演示了靜態(tài)塊和靜態(tài)變量是按照它們?cè)诔绦蛑谐霈F(xiàn)的順序執(zhí)行的。
// 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
內(nèi)部靜態(tài)塊
a 的值:20
從主
當(dāng)使用static關(guān)鍵字聲明方法時(shí),它被稱為靜態(tài)方法。靜態(tài)方法最常見的例子是main()方法。如上所述,可以在創(chuàng)建其類的任何對(duì)象之前訪問任何靜態(tài)成員,而無需引用任何對(duì)象。聲明為靜態(tài)的方法有幾個(gè)限制:
他們只能直接調(diào)用其他靜態(tài)方法。
他們只能直接訪問靜態(tài)數(shù)據(jù)。
他們不能以任何方式引用this或super。
下面是演示靜態(tài)方法限制的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: 錯(cuò)誤:無法從靜態(tài)上下文中引用非靜態(tài)變量 b
b = 10; // 編譯錯(cuò)誤
^
prog.java:22: 錯(cuò)誤:無法從靜態(tài)上下文中引用非靜態(tài)方法 m2()
m2(); // 編譯錯(cuò)誤
^
prog.java:25: 錯(cuò)誤:無法從靜態(tài)上下文中引用非靜態(tài)變量 super
System.out.println(super.a); // 編譯器錯(cuò)誤 ^
prog.java:25: 錯(cuò)誤:找不到符號(hào)
System.out.println(super.a); // 編譯器錯(cuò)誤 ^
符號(hào):變量a
4個(gè)錯(cuò)誤
對(duì)所有對(duì)象共有的屬性使用靜態(tài)變量。例如,在 Student 類中,所有學(xué)生都使用相同的大學(xué)名稱。使用靜態(tài)方法來更改靜態(tài)變量。
考慮以下 java 程序,它說明了靜態(tài)關(guān)鍵字與變量和方法的用法。
// 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();
}
}
輸出
姓名:愛麗絲
卷號(hào):1
clg名稱:XYZ
姓名:鮑勃
卷號(hào):2
clg名稱:XYZ
以上就是關(guān)于“Java中static關(guān)鍵字的作用”介紹,大家如果對(duì)此比較感興趣,想了解更多相關(guān)知識(shí),不妨來關(guān)注一下本站的Java教程,里面還有更豐富的知識(shí)等著大家去學(xué)習(xí),相信對(duì)大家一定會(huì)有所幫助的。
相關(guān)閱讀
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)