2

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.

1
  • Why the vote to close? This is totally a real question. Commented Sep 7, 2010 at 17:43

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I edited the question. I will need to be able to update the var. Thanks for looking at my question.
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.
2

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.

5 Comments

Vote up for mentioning Singletons!
I wouldn't say there is a need to implement a singleton if it will only store data.
@Bozho A settable global variable is one of the worst things you could possibly do, at least a singleton gives you some control over the variable. A properly implemented singleton (one where the code that manipulates the variable is placed into the singleton object itself) is much better. My assumption is that creating the singleton will give Chad a place to put some code and his design skills will evolve. Creating a global variable is going to cause his code to bleed duplication & copy/paste all over. I haven't even mentioned the probable threading issues that can occur.
a private static variable with static setters and getters would give you the same control, with no need to make an instance.
@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.
1

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.

Comments

0

Its not going to win any awards but this should work:

package mypackage;

public class MyGlobal { public static String MY_GLOBAL_VAR = "my variable"; }

Any class within that JVM instance would be able to access MyGlobal.MY_GLOBAL_VAR.
Updated to allow update.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.