12

Why there are no global variables in java? If I like to use any variable in all classes of a program then how can I do that?

2
  • 10
    Global variables will ultimately bring about the destruction of man... or at least my desk, my chair, my monitor and my hair. Commented Jan 28, 2010 at 21:53
  • My hair was already destructed long ago. Commented Jan 24, 2013 at 1:47

10 Answers 10

44

If you really want to do that, make it a public static variable.

However, you'd be advised to try not to - it makes for less elegant, harder to maintain, harder to test code.

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

I have done my duty as a Stack Overflow community member, I up-voted Jon Skeet!
Jon Skeet doesn't need the community; the universe upvotes him plenty already.
Do Jon Skeet, Jeff Atwood and Joel pay you to adulate them?
Do you have any sense of humor ?
10

Global variables (in the Java context - public static variables) are bad, because:

  • harder to maintain - you can't put a breakpoint or log each change to a variable, hence unexpected values at runtime will be very hard to track and fix

  • harder to test - read Miško Havery's post

  • harder to read - when someone sees the code he'll wonder:

    • where does this come from?
    • where else it is read?
    • where else it is modified?
    • how can I know what's its current value?
    • where is it documented?

To make one clarification that seems needed - variables != constants. Variables change, and that's the problem. So having a public static final int DAYS_IN_WEEK = 7 is perfectly fine - no one can change it.

Comments

9

Some valid global "variables" are constants ;-) for example: Math.PI

real-life, but not quite relevant ;)
What if the value of pi changes?
then global variables will be our least problem
It's a defined constant just in case it does change. Then there's only one place to make the change, rather than going through and replacing all the 3.0 references with 3.22114 (or whatever it changes to)! :-)
@Brian: note that that's not true, when it's defined as a compile-time constant (which I think it is). In this case changing the definition won't change anything that was compiled before the change, because the value will the be inlined instead of read from this variable.
7

C++ is multi-paradigm, whereas Java is pure "almost exclusively" an object orientated. Object orientated means that every piece of data must be inside of an object.

Please see the links for more information.

A devil's advicate: static members are not inside of objects :)
They belong to Classes, which are indeed Objects.
Ummm... I thought they were clabjects!
Fair. I've adjusted it to use the same wording as the linked Wikipedia article.
5

Why globals are evil, explained here.

Comments

2

If you need a certain resource that is accessed from any part of your program, have a look at the Singleton Design Pattern.

The "Singleton Is Evil" crowd has picked up your scent and will be knocking on your door shortly. I suggest not answering.
2

I just wanted to add that if you want to use a constant global variable its safe. To use it use the final keyword. Example:

public static final int variable;

And don't assign it a mutable object.
1

Global variables do not jive well with OOP. Still one can have constants as global variables. In a sense, singleton are global variables. If you want to have constants as global variables it is better to use enums in stead.

EDIT:

Constants which used to invoked as Class.Constant now can used Constant by using static import. This comes as close as possible to global variables in C++.

Comments

0
package foo;
public class Globals {
  public static int count = 3;
}

Then, you can access it anywhere as

int uses_global = foo.Globals.count + 1;

Comments

0

Java is intended to make fully-portable, fully-reusable objects.

By definition, something that depends on a "global variable" is not fully portable or fully reusable. It depends on that global variable existing on the new (reusing) system, and it depends on code on the new (reusing) system manipulating that global variable. At that point, you're better off putting that global variable into an object of its own.

I can understand the relationship between reusability and encapsulation (as opposite of global-ity). But how is globality related to portability?
@CesarGon: What happens when you port an object that uses a global variable, say MyBigFatVariable, to a new system, that is using global variable with the same name for a completely different purpose (and maybe it has a completely different type)? (Experienced software professsionals call this a "train wreck". Recommended tools for experiencing such an event include a lawn chair, a cooler full of beer, a large box of popcorn, and a good vantage point. Recommended protocol for experiencing one is to be well away from it when it happens.)
Okay, I understand. The problem is not with the porting itself, but with the integration of a piece of code into a new environment. It may well be a new environment in the same platform. Portability is about crossing platform boundaries. Hence my confusion. But thanks for the explanation. :-)

Your Answer

Draft saved
Draft discarded

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.