Page 1 of 1

Can i use This operator on a static variable?

Posted: Mon Jun 03, 2013 11:51 am
by deepa.patre
Can i use This operator on a static variable?

In this Code:

Code: Select all

public class Test{    
int i1;    
static int i2;    
public void method1(){       
int i;       
// ... insert statements here    
} 
}
Can i use i=this.i2; in insert statements here?

Since i2 is a static member and we cannot use this on static...

Please advise!

Thanks,
Deepa

Re: Can i use This operator on a static variable?

Posted: Mon Jun 03, 2013 8:28 pm
by admin
Yes, you can technically use "this" for accessing a static variable but it is misleading. Static variable does not belong to an object of the class but to the class itself. So even though the compiler allows it, it has no meaning.

Re: Can i use This operator on a static variable?

Posted: Thu Jun 06, 2013 3:11 pm
by deepa.patre
Thanks!