首页 > 文章列表 > 在Java中,可以使用"this"关键字来引用静态成员吗?

在Java中,可以使用"this"关键字来引用静态成员吗?

Java中"this"关键字不能用于引用静态成员。
452 2023-08-18

不,"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);
   }
}

输出

Inise the show() method
The value of a is: 50
The value of b is: 55