Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (767)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Static Variables - Im on WinXP and LinuxMint, on Linux i have no problems ??  (Read 2116 times)
0 Members and 1 Guest are viewing this topic.
Offline NewOnJava

Junior Devvie




1970 born Java game hobbyer.


« Posted 2010-05-19 04:57:44 »

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..
Offline Riven
Administrator

« JGO Overlord »


Medals: 1356
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-05-19 11:39:23 »

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).

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline JL235

JGO Coder


Medals: 10



« Reply #2 - Posted 2010-05-19 18:49:25 »

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).

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Abuse

JGO Ninja


Medals: 70


falling into the abyss of reality


« Reply #3 - Posted 2010-05-20 22:23:05 »

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.
Offline NewOnJava

Junior Devvie




1970 born Java game hobbyer.


« Reply #4 - Posted 2010-05-21 02:14:37 »

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
1  
public ChessGame_Variables vars = new ChessGame_Variables ();


and at other classes _Init, _GameLogic, _ScreenUpdate i have an contrucstors for vars,
1  
2  
3  
4  
5  
6  
public ChessGame_Variables vars = null;

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


I hope i get it right this time Wink, 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..
Offline JL235

JGO Coder


Medals: 10



« Reply #5 - Posted 2010-05-21 18:59:24 »

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.
I'm surprised they don't load them using seperate class loaders.

Offline kappa
« League of Dukes »

JGO Kernel


Medals: 123
Projects: 15


★★★★★


« Reply #6 - Posted 2010-05-21 19:03:45 »

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
<PARAM name="classloader_cache" value="false">
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1303 views)
2018-06-10 19:43:48

EgonOlsen (1161 views)
2018-06-10 19:43:44

EgonOlsen (900 views)
2018-06-10 19:43:20

DesertCoockie (1301 views)
2018-05-13 18:23:11

nelsongames (1106 views)
2018-04-24 18:15:36

nelsongames (1338 views)
2018-04-24 18:14:32

ivj94 (2080 views)
2018-03-24 14:47:39

ivj94 (1231 views)
2018-03-24 14:46:31

ivj94 (2214 views)
2018-03-24 14:43:53

Solater (798 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!