Can i use This operator on a static variable?

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
deepa.patre
Posts: 15
Joined: Thu Dec 13, 2012 9:44 am
Contact:

Can i use This operator on a static variable?

Post 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

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

deepa.patre
Posts: 15
Joined: Thu Dec 13, 2012 9:44 am
Contact:

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

Post by deepa.patre »

Thanks!

Post Reply

Who is online

Users browsing this forum: likejudo and 226 guests