Static Variables - Im on WinXP and LinuxMint, on Linux i have no problems ??

Hello!

I have an boardgames site on a build.

I have all my game classes ( currently 4 games ) in one applet.

My problem comes when i restart my games, on WinXP my Applet wont work fully after i double choose a game, there is problems on variable updating ??

All my variables are on a static “style”.

In what ways the Linux and Windows static variables differ, im with Sun Java 1.6.0.20.


Thanks…

Never use non-final static variables in applets.

Multiple applets launch in the same VM (unless explicitly instructed not to), so all your applets share the same static variables. Even leaving a page and loading a brand new applet will launch your applet in the VM of the discarded applet, unless some timeout occurred, or you cleared your classloader cache (java console -> type XC).

First only Java decides how static variables are handled (not Windows or Linux). However as Riven pointed out Applets are typically re-used on Windows, but not on LinuxMint, and so you are missing out on what is known as “static initialization”. That is where the static aspect of a class is initialized for the first time; which doesn’t occur if the applet is simply being re-used (as this is more efficient then making a new applet each time). I’d personally suspect skipping this is down to how the browser is handling the JVM, not the OS.

As a quick fix you can manually initialize all of your non-final static variables in the Applet’s start method (maybe in init too, but I’m not sure if this will only be called once when it is created).

Bad idea; 2 instances of said applet running concurrently will be sharing the same static variables.
That leaves the door wide open to a world of painful-to-find bugs.

Even using statics in an application can cause problems.

Use them in an application -> fine.
Application code gets refactored into a library for reuse -> fine.
Library gets used by an Applet -> oh dear.

Best advice is to not use static variables. Ever.

StaticVariables!

I have now removed all, i think, static variables from my code, thank you for pointing me my bug, i only had 4 game demoes to track down,
i will later on build atleast 30 AWT games to my site.

Im only a hobbyer with Java, i have no school history on computers.

so, i removed all static variables and changed my code to following style.

My chessgame for example holds 5 classes.

ChessGame.java,
ChessGame_Variables.java,
ChessGame_Init.java,
ChessGame_GameLogic.java,
ChessGame_ScreenUpdate.java.

My variables are all on ChessGame_Variables.java file.

I call it as following ->

at ChessGame.java i have

public ChessGame_Variables vars = new ChessGame_Variables ();

and at other classes _Init, _GameLogic, _ScreenUpdate i have an contrucstors for vars,


public ChessGame_Variables vars = null;

public ChessGame_XXXXX ( ChessGame_Variables fvars )
{					
    vars = fvars;
}

I hope i get it right this time ;), i will work like this way on my futher releases too !!
It was a bit of a dump idea from me to try to build my Applets with static variables !!


Thanks…

I’m surprised they don’t load them using seperate class loaders.

simple fix, add the following parameter in your applet tag and it’ll load it in a new classloader as opposed to sharing one.

[quote]
[/quote]