Can a "this" keyword be used to refer to static members in Java?

JavaObject Oriented ProgrammingProgramming

No, the "this" keyword cannot be used to refer to the static members of a class. This is because the “this” keyword points to the current object of the class and the static member does not need any object to be called. The static member of a class can be accessed directly without creating an object in Java.

Example

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);
   }
}

Output

Inise the show() method
The value of a is: 50
The value of b is: 55
raja
Published on 03-Sep-2019 12:49:40
Advertisements