File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why there are no global variables in Java?

 
Aashu Mahajan
Ranch Hand
Posts: 114
Eclipse IDE Java Oracle
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why there are no global variables in Java?
 
Paul Clapham
Sheriff
Pie
Posts: 20183
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the designers of the language didn't include that feature.
 
Vijay Tidake
Ranch Hand
Posts: 146
Hibernate Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Clapham
Sheriff
Pie
Posts: 20183
25
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
Hibernate Java Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20183
25
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually Java does.

Try System.setProperty and System.getProperty.
 
Ahmad Yazdankhah
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can you define your main class private?!
I think your main class must be public.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic