Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Static variables problem

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am having a static variable in a class,

private static boolean schemaCreated;

Inside the contructor of the class, I have


Basically I am trying to create a singleton, but whever I create a new instance, 'schemaCreated' is false and it goes into the condition.

This class is created from two different classes.
I am using Eclipse 3.1. Does eclipse create a different JVM sandbox for each run?.

What is the lifetime of a static variable?.

Please let me know what I missed.

Thanks.
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's much better ways to implement Singleton. Without lazy initialization use declare a static variable and use a static variable initializer to create it:



If you require lazy initialization use an Initialization On Demand Holder:



If you need to perform a more complicated initialization you could use a static initializer (as opposed to a static variable initializer) to do it, just follow the same idiom and instead of using the variable initializer do it in a static block. Lastly, if you need to handle errors or allow them to propogate up through getInstance() from initialization simply go the route of synchronizing the entire getInstance() method:

 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also suspect, based on your statement about "sandboxes" and "runs", that you're expecting static variables to be shared across Java processes. They're not. Every time you run a Java program, from the command line, from double-clicking a JAR file, or from choosing "Run..." in Eclipse, you get a new JVM, which is a new OS process, and which has a completely distinct separate set of all variables and data, unrelated to any process before or after.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the lifetime of a static variable?
It is created when the first time class loaded, and stays there until your application ends.
 
Thirumurugan Mylrajan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your replies. .I am from the mainframe world, trying to look outside the "well".

By Application I assume every "Run" or "java" command invocation.

I was trying to "Simulate" a multithreaded behaviour by separate runs. Looks like there is no other alternative but to create separate threads from a single class.

I am trying to Sync threads using a static singleton. I am not able to predict any future pitfalls. Can someone suggest if this approach is ok.

Thanks.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the Singleton examples I posted are thread-safe. What you mean by "sync threads by using a static singleton" I'm not sure. There is a threading and synchronization forum here if you have other questions about multithreading.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic