File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Critical BUG from SUN !!! Static variable is not STATIC any more !!!!!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Critical BUG from SUN !!! Static variable is not STATIC any more !!!!!!!!!" Watch "Critical BUG from SUN !!! Static variable is not STATIC any more !!!!!!!!!" New topic
Author

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

YuenLian Wu
Ranch Hand

Joined: Nov 16, 2005
Posts: 73
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

Joined: Feb 26, 2005
Posts: 2053
You mean, the value returned is always "AAAA0001"?
YuenLian Wu
Ranch Hand

Joined: Nov 16, 2005
Posts: 73
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

Joined: Feb 02, 2006
Posts: 2023
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

Joined: Oct 15, 2005
Posts: 163
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

Joined: Oct 15, 2005
Posts: 163

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

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I don't see any static variable in the code you posted.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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 ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: Critical BUG from SUN !!! Static variable is not STATIC any more !!!!!!!!!