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
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/

share this post

Share on twitter
Share on facebook
Share on linkedin
Share on email
 let's have a proper conversation contact us

let's have a proper conversation

more to explore

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.

productions

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

systems

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.

campaign

we design and manage effective campaigns that exceed set targets

info@colonelduck.co.uk
0121 426 3776
Studio 2, Weekin Works
112-116 Park Hill Road
Birmingham
B17 9HD

This site uses cookies: Find out more.