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


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

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

Updated on: 29-Nov-2023

472 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements