tags:

views:

101

answers:

4

I have an odd situation where i want to be able to be able to persist a variable in memory.. like a global variable I can pin in the JVM.

Is this possible? I remember doing something similar in college, but can't find it by googling. I have a logic problem that has some artificial constraints that make this the best possible solution.

EDIT 1:

I will need to update the value of the variable.

EDIT 2 :

I appreciate the responses guys. I'm a .net programmer and hadn't used java since college. Thanks again.

+5  A: 

Yes, using a static field:

public class GlobalVariableHolder {
    public static int globalVariable;
}

Note, however, that this is considered a bad practice and can lead to unexpected results that are hard to debug. The way to not use a global variable is to pass it around as an argument or methods where you need it.

If you are still sure you need this, in order to guard yourself as much as possible, use synchronization. Even better, if the variable is going to be primitive (int, long, etc), you can use AtomicInteger's getAndAdd() or addAndGet() method.

Bozho
I edited the question. I will need to be able to update the var. Thanks for looking at my question.
Chad
You need to beware that the static member will be local to the ClassLoader! J2EE stacks can domain applications and contexts into their own class loader hierarchy. Ensure the class is loaded at the top-level.
Xepoch
+1  A: 

Do you mean something that will exist across multiple invocations of java.exe, or do you mean a single variable that will be the same location in memory regardless of which thread within java.exe access it? Or do you mean a variable that can only be accessed if you're using JRockit? Or maybe just the JVM on your dev machine, but not on another system?

In the first case, you'd need another way to store it, like a config file.

In the second case, like Bozho says, use the static keyword.

In the third case, you'd probably need to use the System class and determine the JVM manufacturer (Assuming that's available from System - I'm not sure off the top of my head, and you'll learn more by looking up the API yourself).

In the fourth case, you're pretty much back to a config file.

atk
A: 
Mondain
no need to make it upper-case - it's not a constant.
Bozho
True, but it was constant before I edited it :)
Mondain
+2  A: 

Usually you will end up storing these things in some kind of a global class--a class that is accessible from anywhere and has a controlled number of instances.

Singletons are commonly used for this. If you look up the pattern for a singleton and store your variable in that singleton (add a setter and a getter) you are on your way.

Doing this (as opposed to a public static variable) will give you some level of access control and traceability--for instance you can put debug statements in the getter if you find you are getting unpredictable results.

In the long run setters and getters and singletons are all bad code smells but no where near as bad as a settable public variable.

Later you may want to move the code that manipulates that variable into the singleton object and possibly convert the singleton to something you can fetch via IOC, but having a singleton is a much better place to start than with a public static.

Bill K
Vote up for mentioning Singletons!
CoolBeans
I wouldn't say there is a need to implement a singleton if it will only store data.
Bozho
Bill K
a private static variable with static setters and getters would give you the same control, with no need to make an instance.
Bozho
@Bozho this is another horrid pattern although I didn't realize it until I had to fix my own code that did this. When it's a singleton object you can replace it with mock objects and have additional instances via IOC pretty easily, when you used the lazy shortcut of public methods you create a nasty little static class that needs lots of refactoring across all your code to do anything with. Just as a piece of advice, avoid this pattern and use Singletons to save yourself some trouble you may not currently be equipped to foresee. It's like an extra 2 lines of code and there is no drawback.
Bill K
The mock is a good point.
Bozho