Chapter 4.3.6 – Global and Local Variables | Introduction to Programming Using Java

Chapter 4.3.6 – Global and Local Variables | Introduction to Programming Using Java

 

4.3.6 Global and Local Variables

 

I’ll finish this section on parameters by noting that we now have three different sorts of variables that can be used inside a subroutine: local variables declared in the subroutine, formal parameter names, and static member variables that are declared outside the subroutine but inside the same class as the subroutine.

 

global and local variables

 

Local variables have no connection to the outside world; they are purely part of the internal working of the subroutine. Parameters are used to “drop” values into the subroutine when it is called, but once the subroutine starts executing, parameters act much like local variables. Changes made inside a subroutine to a formal parameter have no effect on the rest of the program (at least if the type of the parameter is one of the primitive types—things are more complicated in the case of objects, as we’ll see later).

Things are different when a subroutine uses a variable that is defined outside the subroutine. That variable exists independently of the subroutine, and it is accessible to other parts of the program, as well as to the subroutine. Such a variable is said to be global to the subroutine, as opposed to the local variables defined inside the subroutine. The scope of a global variable includes the entire class in which it is defined. Changes made to a global variable can have effects that extend outside the subroutine where the changes are made.

 

Chapter 4.3.6 - Global and Local Variables | Introduction to Programming Using Java

 

You’ve seen how this works in the last example in the previous section, where the value of the global variable, gamesWon, is computed inside a subroutine and is used in the main() routine.

It’s not always bad to use global variables in subroutines, but you should realize that the global variable then has to be considered part of the subroutine’s interface. The subroutine uses the global variable to communicate with the rest of the program.

This is a kind of sneaky, back-door communication that is less visible than communication done through parameters, and it risks violating the rule that the interface of a black box should be straightforward and easy to understand. So before you use a global variable in a subroutine, you should consider whether it’s really necessary.

 

 

java 1 Chapter 4.3.6 - Global and Local Variables | Introduction to Programming Using Java

 

 

I don’t advise you to take an absolute stand against using global variables inside subroutines. There is at least one good reason to do it: If you think of the class as a whole as being a kind of black box, it can be very reasonable to let the subroutines inside that box be a little sneaky about communicating with each other, if that will make the class as a whole look simpler from the outside.

 

Chapter 4.3.6 - Global and Local Variables | Introduction to Programming Using Java

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 4.3 – Parameters | Introduction to Programming Using Java

Chapter 4.3.2 – Formal and Actual Parameters | Introduction to Programming Using Java

Chapter 4.3.3 – Overloading | Introduction to Programming Using Java

Chapter 4.3.4 – Subroutine Examples | Introduction to Programming Using Java

Chapter 4.3.5 – Throwing Exceptions | Introduction to Programming Using Java

1 thought on “Chapter 4.3.6 – Global and Local Variables | Introduction to Programming Using Java”

Leave a Comment