What are global variables?

Colonel Duck
Quacking tips
Published in
3 min readJul 16, 2018

Scope and visibility

In the Java programming language, variables have Scope. The scope of a variable is the area of the code where the variable is useable. Outside of a variable’s visible range, it cannot be referenced or seen.

A set of two open-closed braces are called a Code Block. The variable’s scope is defined by:

1. The Code Block the variable is defined inside.

2. The line on which the variable is defined.

Above the variable’s definition line, or outside of that Code Block, the variable can’t be used.

With great scope, comes great responsibility

You can define variables with global scope. We call these global variables. They can be read, modified or set from any part of the program. If the program could benefit from repeatedly defining the same variable in different places, it would make sense to define it only once as a global variable.

Global variables should however be used sparingly. Leaving a delicious unattended samosa where anyone can grab it is bad practice. Since it has global scope, you might come back to find it eaten or tampered with.

Even worse, a common problem in a multithreaded(multi-person) environment is that other people can modify global variables at any time.

Imagine picking up your samosa, bringing it to your mouth and having it disappear mid bite.

Securing your Samosa

Immutable variables are variables that can’t be changed. Immutable global variables can safely have global scope since methods that rely on that global variable won’t become unpredictable.

At a closed tv store, the television behind glass can be watched by many people at the same time. This is considered thread safe since nobody can change the tv channel.
Watching that tv doesn’t affect another person’s ability to watch it.

Testing and global variables

In the following image, we consider some code whose function depends on the current date.

We get the current date from the calendar global variable and throw a party if it is someone’s birthday. It would be difficult to make sure this code works properly since it does different things on different days.

Using Dependency Injection, we can change the code to allow us to specify the current date each time. This allows us to test the code much more easily.

If you’re interested in finding out more, consider the links below. I found Dependency Injection Demystified very short and clear. I highly recommend “Singleton I love you, but you’re bringing me down” as the writer has incredible style.

· Global Variables — Singleton I love you, but you’re bringing me down
http://geekswithblogs.net/AngelEyes/archive/2013/09/08/singleton-i-love-you-but-youre-bringing-me-down-re-uploaded.aspx

· Dependency Injection — Dependency Injection Demystified
http://www.jamesshore.com/Blog/Dependency-Injection-Demystified.html

· Façade Service Refactoring — Refactoring to Aggregate Services
http://blog.ploeh.dk/2010/02/02/RefactoringtoAggregateServices/

--

--

Colonel Duck
Quacking tips

Award winning software and creative agency — creating value differently.