0 votes
in JAVA by
Can this keyword be used to refer static members?

1 Answer

0 votes
by
Yes, It is possible to use this keyword to refer static members because this is just a reference variable which refers to the current class object. However, as we know that, it is unnecessary to access static variables through objects, therefore, it is not the best practice to use this to refer static members. Consider the following example.

public class Test   

{  

    static int i = 10;   

    public Test ()  

    {  

        System.out.println(this.i);      

    }  

    public static void main (String args[])  

    {  

        Test t = new Test();  

    }  

}  

Output

10

Related questions

0 votes
asked Dec 7, 2020 in JAVA by SakshiSharma
+1 vote
asked Jul 27, 2021 in JAVA by SakshiSharma
...