After reading global-variables-in-java, I found it's not suggested to declare global variables. However, what's a better approach if I want to declare constant variables viewed by all methods in all classes. How to make those variables' scope global? How and why, thank you!
-
Why didn't the answers in that question work for you? It sounds like you're saying "I found it's not suggested to declare global variables, but I want to declare them anyways. How do I do that?" – awksp Jun 12 '14 at 02:23
-
There is no way to access it without it being static, instead of passing each instance of your global class to each of the class you want to access it. – Rod_Algonquin Jun 12 '14 at 02:25
-
I just wonder that if there exist better approach than declaring global varaibles. – Alston Jun 12 '14 at 02:25
-
1Not declaring global variables, perhaps? It's hard to say what's "better" without more context. – awksp Jun 12 '14 at 02:26
-
@Rod_Algonquin I really want to avoid that! – Alston Jun 12 '14 at 02:26
-
2Add a code sample for which you need an alternative and why you think you need an alternative. – Java42 Jun 12 '14 at 02:26
-
1Java doesn't have global variables, but it has static fields, which are basically the same. – user253751 Jun 12 '14 at 02:27
-
@Java42 The reason I want an alternative approach is because someone in that article complains about that but without specific explanation. I think if there are constant variables and then we shouldn't worry about global variables issues... – Alston Jun 12 '14 at 02:33
1 Answers
if I want to declare constant variables viewed by all methods in all classes
If those are really constants, then public static final
fields are perfectly fine.
What kind of data is this? Does it need to externally configurable (at run time or build time)? Does it need to computed at application startup? Does that computation require external resources (such as files or networks) that may be unavailable or slow?
Well, the variables are indicating the state, in different drawing state(draw, erase...).
Not sure I understand what that means exactly, but maybe you want to define an enum
?
I don't want to pass lots of parameter each time
That is a bad argument to use global variables.
Maybe you want to combine multiple arguments in a holder object?
Or have stateful objects with methods (as opposed to stateless functions that take many parameters).
But I still have to
new
a class containing these constants.
No. You can do
public final class MyConstants{
public static final String MY_OAUTH_KEY = "ABCDEFGH";
// maybe this should come from pom.xml
public static final String APP_VERSION = "0.0.1";
}
and then use it from anywhere in your code
System.out.format("You are running version %s%n", MyConstants.APP_VERSION);

- 257,207
- 101
- 511
- 656
-
But I still have to new a class containing these constants. Thus other class can view it. What's the difference between it and global variables? – Alston Jun 12 '14 at 02:29
-
Well, the variables are indicating the `state`, in different drawing state(draw, erase...). I don't want to pass lots of parameter each time. Could I ask that if the data are mentioned as your answer (externally configurable ...), would it influence the design? I don't really get it what are the situations you mention in the answer: `externally configurable`, `application startup` and so on. – Alston Jun 12 '14 at 02:38
-
-
Ok, do you encounter situations that this solution is not appropriate? (you can provide me source code or relative links, I will study and ask again) I just want to know more about it. – Alston Jun 12 '14 at 02:46