What are global variables?

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

· Dependency Injection — Dependency Injection Demystified

· Façade Service Refactoring — Refactoring to Aggregate Services

share this post

 let's have a proper conversation contact us
A circle used for styling a banner

more to explore

A circle used for styling a banner

2021 has been an eventful year!

We’re looking back and reflecting on everything we’ve achieved this year. Thank you to everyone who has worked with us this year. We’ve been fortunate

One person standing in a warehouse, surrounded by professional lights, audio equipment, and cameras. This is part of Creative agency, Colonel Duck's Video Production in Birmingham with BVSC to capture real life stories.


we create and deliver captivating content that audiences engage with.

Two computers next to each other, one showing a web application, created by software agency Colonel Duck, called Pre Enrolment, and the other with software related code


we build and evolve robust platforms that people want to use.

The BBC live streaming one of the Multiple Disadvantage day events, a choir singing, in Birmingham Grand Central Station.


we design and manage effective campaigns that exceed set targets

0121 426 3776
Studio 2, Weekin Works
112-116 Park Hill Road
B17 9HD
A circle used for styling a banner
A circle used for styling a banner

This site uses cookies: Find out more.