• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static variables problem

 
Thirumurugan Mylrajan
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • 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.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • 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:

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic