Declaring and using variables is one of the most fundamental aspects of programming.
In this tutorial you’ll learn how to declare and use variables in Java!

What is a variable in Java?

Simply put, a variable is nothing more than a name for a memory location.
Think of a variable as a bucket, which you can  put data into.

Declaring variables in Java

To keep a reference to each and every variable in your code, you assign the variable a name.
The name of the variable can be anything you like, but there are a few rules you must keep in mind;

  • All variable names must begin with a letter of the alphabet, and underscore or a dollar sign.
  • After the first letter of the variable, you can also use numbers, 0-9.

A variable can be anything you like, as you can declare your own objects in Java.
On top of that, Java has also declared a few data types for you (primitives).
Since objects are somewhat more advanced and complicated, we’ll just stick to the primitives for now.

There are eight kinds of standard data types / primitives in Java;

  • byte – Whole numbers from -128 to 127
  • short – Whole numbers from -32,768 to 32,767
  • int – Whole numbers from -2,147,483,648 to 2,147,483,647
  • long – Whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • float – Decimal point numbers from 3.4e−038 to 3.4e+038
  • double – Decimal point numbers from 1.7e−308 to 1.7e+038.
  • char – A single character
  • boolean – True or false

Primitives are a special kind of variables that you can use anywhere you want – you do not have to declare the types first, they ship “out-of-the-box” with Java.

Rules for declaring variables in Java

There are quite a few rules for declaring and using variables in Java.

All variables must be declared with a data type. If you’re just using primitives, this means that a variable must be one of the eight primitive types listed above.

When declaring variables in Java, you must do it this way;

[data type] [variable name];

int a;

That’s how you declare a variable. You can, of course, also assign a value to the variable.

[variable name] = [value];

a = 100;

If you know the value of the variable before-hand, you can declare and assign the variable in one go.

[data type] [variable name] = [value];

int a = 100;

Reserved words in Java can’t be used as variable names.

In Java, and any other programming language, you have what’s called reserved words.
Reserved words are keywords which have some kind of special meaning – for example int, which is a reserved word for the data type primitive data type int.

Variable names should be meaningful!

This is not a rule of the Java programming language, but it as always best practice to name your variables with a clear and descriptive name.
If you were to declare and assign a variable for your current bank account balance, you could do it like this

int a = 10000;

or like this

int bankAccountBalance = 10000;

Clearly, the last variable is a lot more descriptive. When you start working with more and more variables, you’ll be happy that your previous self  gave the variables a sensible name 😉

Java variable types

In total, there are three variable types (not to be confused with data types!) in Java.

Instance variables

Instance variables are variables that belong to the class that declared them.

class BankAccount {
    int balance; // Instance variable!

Local variables

Local variables are variables that are declared within a method or other types of code blocks.

int getBalance() {
     int balance = 100; // Local variable
     return balance;

Static variables

Static variables are variables that don’t belong to any instance of the class.
Static variables belong to the class itself, not the instance.

class Test {
    static int a = 100;

The final keyword

Lastly I want to cover the final keyword in Java.
When you declare a variable using the final keyword, you effectively make that variable unchangeable.

The final keyword is great to use when you have variables you know won’t change.
To declare variable final and assign it a value,  you simply do it like this

final [data type] [variable name] = [value]

final int a = 0;

This code declares a variable named a and assigns it a value of 0.
Since we made use of the final keyword, this variable can never change. Assigning it  a new value simply isn’t allowed;

a = 100; // Not allowed, compiler error!

Now, I encourage you to try declaring a few variables yourself, and see how it plays out!
Feel free to comment with any questions you may have below.

Happy coding! 🙂