- No class is an island.
- There are no silver-bullets, at least its very true in programming.
- Premature optimisation is the root of all evil.
- In Java we don't have global variables. We only have class variables, instance variables, and method variables.
[Edit]
I am trying to explain here my last point. In fact, bringing the discussion, that is going-on in comments below, to the actual post.
First look at this, an SO thread of C#. There folks are also suggesting the same thing, which is,
So, here we go.
retort: Classes are globally scoped, and thus all class variables are globally scoped. Hence should be called global.
counter-retort: Not all classes are globally scoped. A class can be package-private
. Therefore, the static
variables in there will not be visible outside the package. Hence, should not be called as global. Furthermore, classes can be nested, thus can be private
as well and definitely can have some static
variables but those wouldn't be called global.
retort: public
classes are globally scoped, and thus all class variables are globally scoped.
counter-retort: Not exactly. I would like to move the previous argument here but on a variable level. No matter if the class itself is public
. The variables in there can be protected
, package-private
and private
. Hence, static
variables will not be global in that case.
Now, if you like to call public static
variable in public static
class, as global then call it by any means. But consider this, when you create a new ClassLoader
(as a child of the bootstrap ClassLoader
) and load a class that you've already loaded. Then that results in a "very new copy of the class" -- complete with its own new set of static
s. Very "un-global", indeed. However, we don't use the word global in Java because it tends to confuse the things and then we need to come with whole lot of explanations just to make everything clear. Folks rightly like to explain the feature of global
variables in Java by static
variables. There is no problem in that. If you have some problem/code in any other language and that is using global variables and you need to convert that code to Java, then you most likely make use of static
variable as an alternative.
A couple of examples I like to render here
When I started Java, instructors like to explain the difference of passing object type variable and primitive variables. And they constantly use the term objects are pass-by-reference
, whereas primitives are pass-by-value
. Students found this explanation quite confusing. So, we came up with the notion that everything in Java is pass-by-value. And we explain that for objects references are pass-by-value. It becomes much more clear and simple.
Similarly, there are languages which support multiple-inheritance. But Java doesn't, again arguably speaking. But folks tend to explain that feature using interface
s. They explain it by class implementing many interfaces, and call it multiple-inheritance. That's perfectly fine. But what the class, actually, receives by inheriting a number of interfaces. Frankly speaking, nothing. Why?
. Because all the variables in interfaces are implicitly public
, final
and static
, which apparently means those belongs to the class and anyone can access those. Now we can say that perhaps there would be some inner class
in the interface, then the class implementing the interface will have it. But again that will be static
implicitly and will belong to the interface. Therefore, all what the class will get are methods. And don't forget just the definition and the contract which says, "the class implementing this interface must provide the implementation of all methods or declare itself abstract
". Hence, that class will only get responsibilities and nothing much. But that solves our problems in a brilliant way.
Bottom line
Therefore, we say
- There are no global variables in Java
- Java doesn't support multiple-inheritance, but something like that can be achieved by implementing multiple interfaces. And that really works
- There is nothing pass-by-reference in Java, but references are pass-by-value
Now I like to site few more places
- Java does not support global, universally accessible variables. You can get the same sorts of effects with classes that have static variables [Ref]
- However,
extern
in ObjectiveC is not an alternative to a class-scoped static variable in Java, in fact it is more like a global variable … so use with caution. [Ref]
- In place of global variables as in C/C++, Java allows variables in a class to be declared static [Ref]
- Furthermore, the overuse of static members can lead to problems similar to those experienced in languages like C and C++ that support global variables and global functions. [Ref]
All these are inferring one and the same idea. Which is Java doesn't support global variables.
Hell, I wrote that much. Sorry folks.