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

Static variables accessed in an Instance Method

 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from Enthuware (QID: com.enthuware.ets.scjp.v6.2.388).



I answered d because I thought that the code won't even compile because the instance method run is accessing the static variables x and y. My answer is correct but my thoughts didn't match the explanation:


You may be tempted by the synchronized keyword on the run method. But note that, there are two diferent thread objects and both the threads are accquiring locks for their own thread object. So, in this case synchronized doesn't make any sense.



I tried to compile and run the program, and it worked. I'm confused here, how come the static variables x and y were accessed successfully in the run method, which is an instance method? Or am I just seeing it the wrong way?
[ September 08, 2008: Message edited by: Denise Saulon ]
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-------------------
for(; ; ) {}
-------------------

how many time it will run?

Hope This Helps
[ September 08, 2008: Message edited by: seetharaman venkatasamy ]
 
Denise Advincula
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, thank you for your reply.

Yes it's an infinite loop. But should this program compile first before being able to run? Sorry but these static variables are confusing me.
 
Ranch Hand
Posts: 349
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Denise,

If suppose if the for loop is like
. Then you will get

1 1
2 2
3 3
4 4

printed as both x and y are static and accessed from two different threads both threads will use the same reference of x and y, and when x and y gets incremented by 1 from thread 1 the thread 2 will get x+1 when it access x.

x and y are members of class Test and run belongs to the same class. What makes you think x and y can not be accessed by run method?

Ananth Chellathurai
[ September 08, 2008: Message edited by: Ananth Chellathurai ]
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I tried to compile and run the program, and it worked. I'm confused here, how come the static variables x and y were accessed successfully in the run method, which is an instance method? Or am I just seeing it the wrong way?



I think you may be "remembering" what you learned the wrong way. It is perfectly fine for non-static methods to access static variables. Java doesn't let you access instance variable from static methods, not the other way around.

Henry
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason the answer is "d. You cannot say anything about the values" is due to threading. The synchronization on the run() method doesn't really do anything. They are separate instances, hence, separate locks.

So, a race condition exist between the two threads that access the static variables. And since increment operations are not atomic, the increment may sometimes not work correctly -- hence, producing values that "you cannot say anything about".

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

Originally posted by Henry Wong:


I think you may be "remembering" what you learned the wrong way. It is perfectly fine for non-static methods to access static variables. Java doesn't let you access instance variable from static methods, not the other way around.

Henry



Thank you guys. Actually it's really the static variables that are confusing me. Yes, I must have remembered it the wrong way. I'm trying to search K&B about static variables being accessed in an instance method. I couldn't find it yet but thanks again for informing me anyway.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static variables can even be accessed with the reference of the class.
ex: objref.staticVariable = value; //valid
Only think that you need to remember is that all the references of the object will access the same static variable.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic