aspose file tools*
The moose likes Beginning Java and the fly likes In java global variable are not used because Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "In java global variable are not used because " Watch "In java global variable are not used because " New topic
Author

In java global variable are not used because

jacob deiter
Ranch Hand

Joined: Apr 02, 2008
Posts: 576
In java global variable are not used because

“The global variables breaks the referential transparency”

can anyone explain what is "referential transparency”
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3858

WikiPedia has a good explanation.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

jacob deiter wrote:In java global variable are not used because ....



Though a global variable(one which is not enclosed in any function or class) is hard/impossible to define in Java you can always create a properly scoped, shared variable using a public class and a public static member.



I am not sure of the exact reason why global variables were excluded from Java; but IMO global variables makes understanding and maintenance of your programs difficult. The variables on which your logic depends are scattered and open for modifications. One of the features of Object Oriented programming is to keep data and the functions that operate on the data together (to achieve high degree of cohesion); global variables kind of defeats this, doesn't it?

I neither know what referential transparency means nor am I sure if my reasoning is correct; just wanted to give it a shot though

Cheers!


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Monu Tripathi wrote:. . .

I am not sure of the exact reason why global variables were excluded from Java; but IMO global variables makes understanding and maintenance of your programs difficult. . . . .
You mean public static final int . . . surely?

You said yourself global variables make understanding and maintenance difficult; I am sure that is why they were excluded. Also they are error-prone, and are not associated with objects, and are therefore not object-oriented programming. The only instance where a global value can be safe is where it is used as a constant. Hence "final".
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Campbell Ritchie wrote:You mean public static final int . . . surely?
......
......
The only instance where a global value can be safe is where it is used as a constant. Hence "final".


In C, global variables aren't necessarily constants; I was trying to emulate that aspect in my Java code. That said, I agree with you in that such global variables should be constants(therefore, final) for safety.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
Rahul Kesharwani
Greenhorn

Joined: Jun 04, 2012
Posts: 15

Guys I am still confused with the answers, so I tried to conclude on my own. I don`t know whether I am right or not, but here is what I understood (Please correct me If I am wrong):

Imagine a real life scenario. In an office there is a coffee machine, which is accessible to everyone (all people). Now, since it is accessible to everyone so anyone can do anything to the milk in coffee machine (assume that someone added lime juice to milk). In order to prevent this we have to take some measures (like installing a CCTV to keep mischief out). But we are an indefinite specie, and can always find a way to sour the milk, so we can say, its kind of difficult to always keep an eye on the Coffee machine.

Also, there is always an option that we do not provide Coffee machine at all in the office, but if you wish to have a coffee, you can approach a coffee shop (As in case of Java, no global variables are allowed but you can create public class with static variable)

Now, taking this concept to our Java or C++, both are free to either implement or not implement Global variables, but Java chooses not to. Why?
• One, They are difficult to understand (as we have to look at the whole system, in above example we have to keep an eye to the behaviour of each and every person in the office) and maintain (as for the reasons stated in above example)
• Two, One of the features of Object Oriented programming is to keep data and the functions that operate on the data together (basically Encapsulation, to achieve high degree of cohesion), and global variables kind of defeats this.
• Three, they are error-prone (as souring of milk in above example), and are not associated with objects, and are therefore not object-oriented programming.
The only instance where a global value can be safe is where it is used as a constant.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8402
    
  23

Rahul Kesharwani wrote:Now, taking this concept to our Java or C++, both are free to either implement or not implement Global variables, but Java chooses not to. Why?

I'm not quite sure why you think this is just a Java thing. You can define a global variable in Java just the same as you can in any other language. And they're not just bad in Java, they're bad anywhere.

One of the first books I read when I was learning C about 30 years ago advised you to avoid them "at all costs". And C is a language that makes it more difficult to avoid them (at least you have to think more carefully about how to code an equivalent alternative).

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40029
    
  28
I think the problem is like your milk. You are not necessarily in a well‑behaved office. You have several offices. You cannot predict what people will do with the milk. If you have milk inside the coffee machine, it is encapsulated and protected but if you have a bottle of milk on the shelf, somebody can get their own kettle in the next office and take the milk to their coffee and forget to bring it back. Or leave it out of the fridge until it sours. The problem with global variables like that is that any code can change their values and there are no controls over such behaviour.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: In java global variable are not used because