Browse Today's favorites or All time favorites.
Please Sign In or Register to share and submit your questions.
Java Queries
Complete java interview questions library
Home » Interview Questions » Core Java Interview Questions » Why are there no global variables in Java?
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.
Learn all you can
Learn all you can about the company or organization; learn as much as you can so that your questions are sophisticated and knowledgeable during the interview. Employers expect you to arrive knowing background information about the organization
Write a comment