Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

this for static variables

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jay...
I see your point
Let me tell you couple of things about static
You can access a static variable
1. class reference
2. Thru object reference
The second case is what happening here
that's why you were able to access the static variable thru a this reference
But here is the important point, "this" accessibilty will fail if the context is static ie in the second case its static main
I hope it helps
Ragu
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
It will complie correctly but if you try to print the static variables in the local class then it will rise an error.
Example
public class ThisUsage
{
int planets;
static int suns;
public static void main(String args[]) {
System.out.println("This reference");
ThisUsage g = new ThisUsage();
g.gaze();
}
public void gaze()
{
int i;
//this.suns = planets; //This will neatly compile
System.out.println(this.sun);//This will cause an error as follows
}
}
/* Error Message -: C:\WINNT\Profiles\nisheeth\Desktop\Java.java\ThisUsage.java:16: No variable sun defined in class ThisUsage.
System.out.println(this.sun);
^
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nisheeth,
The example you used fails due to a typo
<code>System.out.println(this.sun);</code>
should read
<code>System.out.println(this.suns);</code>
The field was declared as 'suns' not 'sun'. If you make the correction the code will compile and run without an error.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited October 22, 2001).]
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys.
-Jay
reply
    Bookmark Topic Watch Topic
  • New Topic