Why are there no global variables in Java?
Page 1 of 1
Why are there no global variables in Java?
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.
· 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.
Similar topics
» Why are there no global variables in Java?
» Swap variables without using temp
» Java Collection
» What are the differences between C++ and Java?
» Count number of 1's in Java
» Swap variables without using temp
» Java Collection
» What are the differences between C++ and Java?
» Count number of 1's in Java
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|