2

I need to set a boolean variable at runtime that can be accessed by any other class (without having to pass the variable around from class to class). This variable will be a configuration setting, and will depend on some value set by the user at runtime.

I suspect a singleton is the way to go, but having spent the morning reading up on it, I seem to find a lot of examples (& arguments) on the best way to create them, without any mention of how to actually use them.

Is singleton the way to solve this?

If so, can anyone provide an example of a singleton, that can have a boolean value set, that can be accessed statically from any other class?

EDIT: I'm such an idiot (totally overcomplicating the problem, and missing the simplest solution).

public static volatile boolean yourBoolean; Looks like it'll work perfectly

1
  • Please just give it a try and post what you have. stackoverflow is not a code writting machine.
    – dngfng
    Oct 12, 2012 at 12:13

3 Answers 3

12

If you just need a boolean value, there is no need to use a singleton. Just declare a:

public static volatile boolean yourBoolean;

(with the volatile keyword to make sure that all changes made are visible across threads if you are in a multi-threaded environment).

3
  • So why not AtomicBoolean? Actually, I'll ask a question instead. :) Oct 12, 2012 at 12:20
  • @DavidGrant Yes that would work too - AtomicBoolean uses a volatile variable internally. The semantics would be the same.
    – assylias
    Oct 12, 2012 at 12:21
  • Here you go: stackoverflow.com/questions/12859008/… Oct 12, 2012 at 12:25
2

If the variable will contain a configuration setting that won't change during runtime, I suggest you use a final static variable. See the example below:

public class Main {
    public final static String CONFIGURATION_SETTING = "some_setting";
}

You can access this constant by using the following reference:

Main.CONFIGURATION_SETTING

It will be available throughout your solution, as long as you import the Main class.

1

I don't think a singleton is necessarily what you need here - as your question itself states, all that you need is a variable that can be accessed by any other class.

Since you don't want to pass anything round, it must be static - and of course it will have to be public in order for other classes to see it.

So the simplest way to achieve this would be something like the following:

public class Config {
    public static boolean flag;
}

Any class can then read the value as Config.flag.

If you have multiple threads in your application, you'll need to think about thread-safety. For simple, single boolean values you can just mark the field as volatile. But if you're doing something a little more complex, or updating several values at once, you'll need to ensure that these updates are atomic and visible to other threads in an appropriate fashion.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.