wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why there are no global variables in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why there are no global variables in Java?" Watch "Why there are no global variables in Java?" New topic
Author

Why there are no global variables in Java?

Aashu Mahajan
Ranch Hand

Joined: May 27, 2011
Posts: 113

Why there are no global variables in Java?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Because the designers of the language didn't include that feature.
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi
I think, as a java is object oriented language so it need to support encapsulation.

and java models encapsulation by using Classes.So in java everything must be inside the class hence their are no global variables in java.

Thanks


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

But C++ is an object-oriented language, isn't it? And it supports global variables, doesn't it? So there seems to be a problem with your proof that global variables cannot exist in an object-oriented language, Vijay.
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

Hi Paul,
But as I stated java models encapsulation in this way(by keeping everything inside the class),and c++ doesn't
and again modeling encapsulation in this way is up to the java language designers as you stated.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

You can still encapsulate like that in C++, though. And in PHP - another language with globals and classes.

We can only speculate as to why they designed it that way but...there are lots of arguments about global variables. Some people think they're a bad idea (I just Googled "global variable" and the no.2 hit is a page titled "Global Variables Are Bad"). It seems likely that the Java designers had some sympathy with that position.
mallory scala
Greenhorn

Joined: Jun 16, 2011
Posts: 1
Global variables are considered bad form for a variety of reasons: · Adding state variables breaks referential transparency (you no longer can understand a statement or expression on its own: you need to understand it in the context of the settings of the global variables).
· State variables lessen the cohesion of a program: you need to know more to understand how something works. A major point of Object-Oriented programming is to break up global state into more easily understood collections of local state.
· When you add one variable, you limit the use of your program to one instance. What you thought was global, someone else might think of as local: they may want to run two copies of your program at once.
For these reasons, Java decided to ban global variables.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

mallory scala wrote:For these reasons, Java decided to ban global variables.


Those may well be good reasons, but do you have some evidence that they were actually the reasons used by the designers of Java? Public statements to that effect? Documentation which says that? What about some evidence that the designers of C++ and PHP rejected those reasons as not applicable?

I don't mean to dismiss your post as useless. It's a perfectly good point about object-oriented design. It just doesn't answer the original question, which is why Java doesn't have global variables. (You simply claim that it does answer the question without any proof.)
john christopher
Greenhorn

Joined: Jul 28, 2011
Posts: 2
Global variables are globally accessible. Java does not support globally accessible variables due to following reasons:

* The global variables breaks the referential transparency
* Global variables creates collisions in namespace.
Yi Yang
Greenhorn

Joined: Aug 09, 2010
Posts: 2
Actually Java does.

Try System.setProperty and System.getProperty.
Ahmad Yazdankhah
Greenhorn

Joined: Jul 30, 2011
Posts: 8
Java certainly supports global variables but in its own way.

Just define a public static variable in your main class (even though I don't recommend), then all your application can access it.

Does it work fine?
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Ahmad Yazdankhah wrote:Just define a public static variable in your main class....Does it work fine?

Do you mean define a public static variable in a public class? Given that a global variable is one that can be accessed from any scope, it doesn't work if the main class is package-private and not public.
Ahmad Yazdankhah
Greenhorn

Joined: Jul 30, 2011
Posts: 8

Can you define your main class private?!
I think your main class must be public.
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Ahmad Yazdankhah wrote:
Can you define your main class private?!
I think your main class must be public.


A top-level class (which may or may not contain a public static void main(String[] args) method) may be either public, or package-private (no explicit access modifier).

This link explains more.
Ahmad Yazdankhah
Greenhorn

Joined: Jul 30, 2011
Posts: 8

OK, now it's clarified that when you say package-private, you don't mean package or private. That's called default modifier in the most Java documents.
Yes, the class I was talking about must be public in order to be visible by the whole application.
 
Consider Paul's rocket mass heater.
 
subject: Why there are no global variables in Java?
 
Similar Threads
Can u we have global variables in Java!
In java global variable are not used because
Global variables in java
JToggleButtons to display a panel elsewhere on the UI
Global Variables