Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Critical BUG from SUN !!! Static variable is not STATIC any more !!!!!!!!!

 
YuenLian Wu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am very frustrated at static variable recently. I wrote the following program but the static variable can just added up once and then not anymore. For example, when the program just starts, the variable contains the value "AAAA0001", and the value will be added up each time when I call up this function, so that I will increase like "AAAA0002", "AAAA0003"..and so on.

Here is my code:



and I called it from other non-static function and the syntax is like "tran.setTranID(BookStoreUtility.getNextTranID());" during runtime, so it will execute this function many times before it terminated. I use an applet class to invoke the other function to call up this method, is that a big clue ? Since I am new to java, I hope I can get helps from you guys.

myriads thanks

Transistor
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean, the value returned is always "AAAA0001"?
 
YuenLian Wu
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you start the program, it's value can be increased by one only during runtime. That means the next transactionID will be AAAA0002, however, it won't be added after this during runtime. THe function will always return AAAA0002 no matter how many times you will invoke this function. Poor me. ar...ar
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is hard to say what the problem is without seeing the whole code. You may add more traces (print out BookStoreConstant.TransactionID value) and to see when it is changed. Also, you may add "synchronized" to your method.
 
Michael Duffy
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by YuenLian Wu:
Hi all,

I am very frustrated at static variable recently. I wrote the following program but the static variable can just added up once and then not anymore. For example, when the program just starts, the variable contains the value "AAAA0001", and the value will be added up each time when I call up this function, so that I will increase like "AAAA0002", "AAAA0003"..and so on.

Here is my code:



and I called it from other non-static function and the syntax is like "tran.setTranID(BookStoreUtility.getNextTranID());" during runtime, so it will execute this function many times before it terminated. I use an applet class to invoke the other function to call up this method, is that a big clue ? Since I am new to java, I hope I can get helps from you guys.

myriads thanks

Transistor


No one who is new to Java should ever claim bugs in the JVM. It's much more likely that you failed to understand something fundamental if exhibited behavior does not match your assumptions. Check your assumptions first.

And I wouldn't call this thread worthy of the "advanced" section.
 
Michael Duffy
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

and I called it from other non-static function and the syntax is like "tran.setTranID(BookStoreUtility.getNextTranID());" during runtime, so it will execute this function many times before it terminated. I use an applet class to invoke the other function to call up this method, is that a big clue ? Since I am new to java, I hope I can get helps from you guys.


One more thought: perhaps the applet is a big clue. The applet .class file is downloaded from the server and runs in the JVM installed in the client's browser. Static variables are associated with the class, not instances, but when the class is loaded by separate JVMs there's no way for the two to communicate changes to each other. So the static variable in one applet can be updated without notifying another instance running on another client. Does that explain the behavior you observe?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Duffy:
And I wouldn't call this thread worthy of the "advanced" section.


Yes, this sounds more like a beginner's question. Moving...
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any static variable in the code you posted.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the assumption is that it's "BookStoreConstant.TransactionID ".

I'm pretty sure that what's happening is that he expects multiple instances of the applet to be sharing this static variable, but in fact each copy just increments its own instance (the one in its own JVM) of the variable the one time the method is called.
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is very difficult to tell from the code -- but if I had to guess... I am assuming that the "BookStoreConstant.TransactionID" is the static variable, and it is called once per applet.

In this scenario, the reason the changes are not propagated, is because it is changed locally in the JVM of the browser hosting the applet.

[EDIT] Got beaten to the punch again...

Henry
[ February 05, 2006: Message edited by: Henry Wong ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic