| 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: 3670
|
|
|
WikiPedia has a good explanation.
|
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
|
 |
Monu Tripathi
Rancher
Joined: Oct 12, 2008
Posts: 1365
|
|
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] | [My Blog] | [Samuh Varta]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
|
|
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: 1365
|
|
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: 32599
|
|
|
 |
 |
|
|
subject: In java global variable are not used because
|
|
|