Given the following code, which statements can be placed at
the indicated position without causing compile errors?
public class ThisUsage
{
int planets;
static int suns;
public void gaze()
{
int i;
//.... insert statement here
}
}
Select all valid answers:
a) i = this.planet;
b) i = this.suns;
c) this = new ThisUsage();
d) this.i = 4;
e) this.suns = planets;
Answer is a), b) and e)
I can understand a) as "this" points to the instance of object ThisUsage. But, I don't understand how "this" can point to
a static variable int sun. As static variables go by the class name and "this" refers to the instance of the object currently used.
For eg., if the above question had a main,
public static void main(
String args[])
{
ThisUsage current = new ThisUsage();
current.gaze();
}
Here, "this" would reference the instance "current" of class ThisUsage. It perfectly makes sense for this to access
member variable planet of instance "current" through, this.planet
But how is this.sun correct?
Any help is greatly appreciated.
-Jay