Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Classloader and static variables

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Found this here: http://forum.java.sun.com/thread.jsp?forum=30&thread=34027
Initially sounded a bit weird, but probably is correct right? Just sounds a bit strange that two applets fire up 'seemingly independently', but then they affect each other like this. This is almost seems like a variable that should be synchronized, right?
Cheers.


As a really simple experiment, create an applet with a public and static (and non-final) variable. Open up the applet in two separate browser processess. Change the value of the variable in one instance of the applet. When you read the value of the variable in the second instance of the applet, does it recognize that the value has changed? If not, then you are running two different VMs. If so, you are running a single, shared VM. I would bet, in this scenario, you will find that the two applets will not share the same VM.
(Static variables are the key here since the classloader only needs to load a class, and its static variables, once.)

 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Applets aren't covered in the certification exam, so I'm going to move this to the Applet forum.
Corey
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jim
well, it didn't sounded much weired 'coz if we know that class loader loads static fields with class and they remain in memory till the class is there... so if something is static in the applet and if the browser uses same JVM instance to run the applet multiple times then it will share that variable and you are correct in the prediction that "it has to be synchronized access" otherwise you might run into problems.
the interesting thing is to observe what happens on different browsers and in various cases.
Browsers,
1. Netscape 7
2. Netscape 4.7*
3. IE
4. Mozilla
Cases,
1. mutlipe browser windows
2. same applet, twice on one page
3. opening an applet page, closing the browser window and open it again the new browser window...
i bet browsers would proove themselves against us behaving differntly
the important factor is- browsers uses caches for applet and all. so if u have a plug-in then u have to use option "x" (clear classloader cache) to make sure the applet is using the latest classes. now, when we close the browser window and open it again the browser may use the cache only to load the applet second time so we may want to use "x" before typing url again in the brwoser second time...
if you have time please consider above cases and see what you get and let us know about it ...
regards
maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
btw
that option "x" is in "Java Console" (if u run Sun JRE in the browser) in case u r unaware..
regards
maulin
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic