1000 Java Tips ebook
Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java
programming articles and tips. Please take your copy here.
Take your copy of free "Java Technology Screensaver"!.
Q: Why are there no global variables in Java?
JavaFAQ Home » Java IAQ by Peter Norvig
Q: Why are there no global
variables in Java?
Global variables are considered bad form for a variety of reasons:
For these reasons, Java decided to ban global variables.
- Adding state variables breaks referential transparency (you no longer
can understand a statement or expression on its own: you need to
in the context of the settings of the global variables).
- State variables lessen the cohesion of a program: you need to know
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.
This tip is reprinted on JavaFAQ.nu by by courtesy of
Peter Norvig I am
thankful for his important contributions to my site - 21 Infrequently Answered
Java Questions. Alexandre Patchine
Printer Friendly Page
Send to a Friend
Search here again if you need more info!