• 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

doubts about static variables

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my doubt: considering the class below
public class TStatic {
public static String strVar = "First";
public TStatic() {
}
public static void setStrVar ( String s ){
strVar = s;
}
}
Supose I have two other classes C1 and C2, running simultaneously in separate computers acessing the TStatic class in a third computer. In certain time, C1 uses the setStrVar method passing "Second". After that, C2 uses setStrVar passing "Third". Then, C1 retrieves strVar value. Which value C1 would retrive : "Second" or "Third" ? I mean, being a static variable implies in having only one piece of memory for it, regardless how many other classes are manipulating it ? And one class can overwrite what other class has set, in this case of static variables ?
Thanks
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
C1 would retrieve "Third". You are correct that there is at most one variable named strVal. Because of that when C2 sets strVal == "Third" it effectively overwrites the value that C1 set it to.
The only other advise I can give is to write yourself a little app that does just this and test your doubts.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mauricio,
static values apply only to one JVM(assume the same ClassLoader). In your scenario, you've got three different JVMs running on three different machines. By the way, how are your classes C1 and C2 talking to the class on the third computer? RMI? CORBA?
[This message has been edited by Mark Savory (edited March 15, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If TStatic is only running on computer 3 then the static variable only exists on computer 3. SOMEHOW the other 2 computers must be interfacing with computer3, either by invoking remote methods, or sending a broker to do stuff. At any rate the operation to set the static variable will happen on computer 3, so in this scenario they are all playing with the same spot in memory.
I think what Mark is trying to warn you is that if you invoke TStatic on one of the other machines, a DIFFERENT static variable will be created for that machine when it was loaded. (Computers don't read minds - it wouldn't know that some other machine had one loaded ).
 
Mauricio Andrade
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To tell the truth, i'm going to load a similar class in a Oracle 8i database, and use it from packages written in pl/sql. I don't know how the database server resolves the communication among the server and the clients, but I think only one JVM will be initialized. Or may be a JVM by session ?
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a server with oracle on it, and you also have a JVM on the same machine, so that the oracle routines can use it (so far am I close??) Then when the remote clients request data from Oracle, it will go to the JVM on the oracle server, do it's thing, and send back the requested information.
Actually, even if the request came from a remote machine with a JVM running, and an application on the remote JVM sent the request, Oracle doesn't really KNOW that it was a JVM that sent the request, in only knows that "something" asked for data. So it would use the local JVM and life is good .
 
Mauricio Andrade
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Now I know that is related to each JVM. That's it.
Thank You all.
reply
    Bookmark Topic Watch Topic
  • New Topic