aspose file tools*
The moose likes Java in General and the fly likes Static variables problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static variables problem" Watch "Static variables problem" New topic

Static variables problem

Thirumurugan Mylrajan
Ranch Hand

Joined: Jan 26, 2006
Posts: 64

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.


SCJP , SCJD. (IBM 142 in progress).
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jul 08, 2003
Posts: 24166

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.

[Jess in Action][AskingGoodQuestions]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
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

Joined: Jan 26, 2006
Posts: 64
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.

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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.
subject: Static variables problem
Similar Threads
static variables ?
class question
Disadvantages of using static variables unnecessarily
Static variable question
juddiv3 from wsdl to java?