可以在 Java 中使用“this”關鍵字來引用靜態成員嗎?
不可以,“this”關鍵字不能用來引用類的靜態成員。這是因為“this”關鍵字指向類的當前物件,而靜態成員不需要任何物件就可以被呼叫。無需在 Java 中建立物件即可直接訪問類的靜態成員。
示例
public class StaticTest { static int a = 50; static int b; static void show() { System.out.println("Inside the show() method"); b = a + 5; } public static void main(String[] args) { show(); System.out.println("The value of a is: " + a); System.out.println("The value of b is: " + b); } }
輸出
Inside the show() method The value of a is: 50 The value of b is: 55
廣告