Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

In java global variable are not used because

 
jacob deiter
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WikiPedia has a good explanation.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rahul Kesharwani
Greenhorn
Posts: 15
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 49432
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic