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

Jonny
  • 3,807
  • 8
  • 31
  • 48
  • Please just give it a try and post what you have. stackoverflow is not a code writting machine. – dngfng Oct 12 '12 at 12:13

3 Answers3

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).

assylias
  • 321,522
  • 82
  • 660
  • 783
  • So why not `AtomicBoolean`? Actually, I'll ask a question instead. :) – David Grant Oct 12 '12 at 12:20
  • @DavidGrant Yes that would work too - AtomicBoolean uses a volatile variable internally. The semantics would be the same. – assylias Oct 12 '12 at 12:21
  • Here you go: http://stackoverflow.com/questions/12859008/what-is-the-difference-between-using-a-volatile-primitive-over-java-util-concurr – David Grant Oct 12 '12 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.

Hanno
  • 563
  • 5
  • 15
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.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228