• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static variable

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, in the following code :
public class Test {
static int total = 10;
public static void main (String args []) {
new Test();
}
public Test () {
System.out.println("In test");
System.out.println(this);
int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}}}
A.The compiler reports an error at line 2
B.The class will not compile
C.The value 10 is one of the elements printed to the standard output
D.The compiler reports an error at line 9
E.The class compiles but generates a runtime error
Can anyone explain why the answer is C and not D ?
Thanks

Stephane
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stephane Weber:
int temp = this.total;


You don't have line numbers, but I'll presume that this is the line in question.
This works because we're taking the value of a static variable and assigning it to a local variable. The static variable is initialized when the class is loaded so we are guaranteed that this variable contains the value 10 by the time we reach this line.
You get in trouble if you try to reference an instance member from a static context, but that isn't the case here. Rather, we're accessing a static member from a non-static context. That's fine.
Was that what your confusion was about or was it regarding the use of the keyword "this"?
I hope that helps,
Corey
 
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the body of the method, the keyword this can be used like any other object reference to access the object.
In this example,
int temp = this.total;
is same as
int temp = Test.total;
HTH,
murthy
 
Stephane Weber
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed my confusion was that I would have expected :
int temp = Test.total
Now I know it's all the same

Thanks
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic