java中的static關(guān)鍵字主要用于內(nèi)存管理。我們可以應(yīng)用java static關(guān)鍵字在變量,方法,塊和嵌套類中。 static關(guān)鍵字屬于類,而不是類的實(shí)例。
靜態(tài)(static)可以是:
如果將一個(gè)變量聲明為static,它就是所謂的靜態(tài)變量了。
靜態(tài)變量的優(yōu)點(diǎn):
理解不使用靜態(tài)變量的問題
class Student{
int rollno;
String name;
String college="ITS";
}
假設(shè)在一所學(xué)校有500名學(xué)生,現(xiàn)在所有實(shí)例數(shù)據(jù)成員將在每次創(chuàng)建對象時(shí)獲取內(nèi)存。所有學(xué)生都有其唯一的注冊ID:rollno和 name ,因此實(shí)例數(shù)據(jù)成員沒有什么問題。college 指的是所有對象的共同屬性。如果使它靜態(tài)化(使用static關(guān)鍵字修飼),這個(gè)字段將只獲得內(nèi)存一次。
Java靜態(tài)屬性被共享給所有對象。
靜態(tài)變量的示例
//Program of static variable
class Student8 {
int rollno;
String name;
static String college = "ITS";
Student8(int r, String n) {
rollno = r;
name = n;
}
void display() {
System.out.println(rollno + " " + name + " " + college);
}
public static void main(String args[]) {
Student8 s1 = new Student8(111, "Karan");
Student8 s2 = new Student8(222, "Aryan");
s1.display();
s2.display();
}
}
上面代碼執(zhí)行結(jié)果如下 -
111 Karan ITS
222 Aryan ITS
創(chuàng)建對象示例圖如下所示 -

不使用靜態(tài)變量的計(jì)數(shù)器程序
在這個(gè)例子中,我們創(chuàng)建了一個(gè)名為count的實(shí)例變量用來統(tǒng)計(jì)創(chuàng)建對象的數(shù)目,它在構(gòu)造函數(shù)中執(zhí)行遞增。 由于實(shí)例變量在創(chuàng)建對象時(shí)要獲取內(nèi)存,每個(gè)對象都將具有實(shí)例變量的副本,如果它被遞增了,它也不會(huì)反映到其他對象中。所以每個(gè)對象在count變量中的值還是1。
class Counter {
int count = 0;// will get memory when instance is created
Counter() {
count++;
System.out.println(count);
}
public static void main(String args[]) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
}
}
上面代碼執(zhí)行結(jié)果如下 -
1
1
1
計(jì)數(shù)器靜態(tài)變量的程序
如上所述,靜態(tài)變量將只獲取一次內(nèi)存,如果任何對象更改靜態(tài)變量的值,它將保留其值,所有實(shí)例均可訪問同一變量值。
class Counter2 {
static int count = 0;// will get memory only once and retain its value
Counter2() {
count++;
System.out.println(count);
}
public static void main(String args[]) {
Counter2 c1 = new Counter2();
Counter2 c2 = new Counter2();
Counter2 c3 = new Counter2();
}
}
上面代碼執(zhí)行結(jié)果如下 -
1
2
3
如果在任何方法上應(yīng)用static關(guān)鍵字,此方法稱為靜態(tài)方法。
靜態(tài)方法的示例
//Program of changing the common property of all objects(static field).
class Student9 {
int rollno;
String name;
static String college = "ITS";
static void change() {
college = "BBDIT";
}
Student9(int r, String n) {
rollno = r;
name = n;
}
void display() {
System.out.println(rollno + " " + name + " " + college);
}
public static void main(String args[]) {
Student9.change();
Student9 s1 = new Student9(111, "Karan");
Student9 s2 = new Student9(222, "Aryan");
Student9 s3 = new Student9(333, "Sonoo");
s1.display();
s2.display();
s3.display();
}
}
上面代碼執(zhí)行輸出以下結(jié)果 -
111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
執(zhí)行正常計(jì)算的靜態(tài)方法的另一個(gè)示例:
//Program to get cube of a given number by static method
class Calculate {
static int cube(int x) {
return x * x * x;
}
public static void main(String args[]) {
int result = Calculate.cube(5);
System.out.println(result);
}
}
上面代碼執(zhí)行輸出以下結(jié)果 -
125
靜態(tài)方法的限制
靜態(tài)方法有兩個(gè)主要限制。它們分別是:
this和super兩個(gè)關(guān)鍵字不能在靜態(tài)上下文中使用。class A {
int a = 40;// non static
public static void main(String args[]) {
System.out.println(a);
}
}
上面代碼執(zhí)行輸出以下結(jié)果 -
[編譯錯(cuò)誤!]Compile Time Error
為什么java main方法是靜態(tài)的?
這是因?yàn)閷ο蟛恍枰{(diào)用靜態(tài)方法,如果它是非靜態(tài)方法,jvm首先要?jiǎng)?chuàng)建對象,然后調(diào)用main()方法,這將導(dǎo)致額外的內(nèi)存分配的問題。
Java中的靜態(tài)塊主要有兩個(gè)作用:
靜態(tài)塊的示例
class A2 {
static {
System.out.println("static block is invoked");
}
public static void main(String args[]) {
System.out.println("Hello main");
}
}
上面代碼執(zhí)行輸出以下結(jié)果 -
static block is invoked
Hello main
可以執(zhí)行程序沒有main()方法嗎?
答:是的,一種方式是靜態(tài)塊,但在以前舊的JDK版本中,不是在JDK 1.7。
class A3 {
static {
System.out.println("static block is invoked");
System.exit(0);
}
}
上面代碼執(zhí)行輸出以下結(jié)果 -
static block is invoked
在JDK7及以上版本中,輸出將為:
錯(cuò)誤: 在類 Main 中找不到 main 方法, 請將 main 方法定義為:
public static void main(String[] args)