Lesson: Variables

Explore Variables in Java.

Understand what Variables are and why they exist. Understand how variables are defined and used in Java. Understand variable scoping.


A Variable is a memory location, or 'slot', reserved by your program into which you will store data. It is called a variable because we can change it as needed as the program executes. When we define variables we assign them meaningful names instead of having to work with actual memory addresses. Here are some examples:

Notice the Java keyword int. Java is a strongly typed language which means that when a variable is defined, we assign a specific data type to that variable. Different kinds of data will require different amounts of memory space to store. So, when we first create the slot in memory for the variable to use, we must tell the compiler what type of thing we will be putting in that slot. This allows the compiler and the JVM to check that we only put the correct data into the variable and to some extent, that we use it correctly. In the example, the variables are all assigned the int (or integer) data type. (We will discuss data types in depth in the next lesson). When data of the wrong type is stored in a variable, you will encounter an error.

When we define a variable, we can initialize it (put a value in it right away) like we did in the case of myVar2, or we can separate the definition from the initialization do that later in the program in a statement as we did for myVar1. The first line of this code simply told the computer to create a slot in memory called myVar1 that will hold data of type “int”. The last line then says, “put the value 7 into the slot I labeled myVar1”. Before the last line, myVar1 is zero (the default), and when we put something into a variable for the first time, it is called ‘initialization’. Sometimes it is more convenient to initialize our variable as soon as we define it, like we did in the case of myVar2. That line says, “create a slot called myVar2 that can hold an int and put 3 in it”.

Notice that the fourth line of code does not include ‘int’ in front of myVar1. This is because myVar1 has already been defined in the code we have written. It is only necessary to define the type of a variable the first time we create it. Once it has been created, the compiler will have set aside the appropriate space to store things in that location in memory, so we do not have to restate that myVar1 is an integer. It is important not to restate the data type, because doing so will influence the scope of that variable depending on where it is stated (see below).

Variables are used in statements, which are the actual processing steps the program will execute. Variables change by appearing on the left side of the assignment operator  =. So = does not mean “is equal to”, but means “evaluate what is on the right of this symbol and put the result in the variable on the left”. We will discuss statements and operators in more detail a bit later.

Variables can be used below where they are defined but not above and must be used in a method. Variables can only be used inside the code block in which they are defined. This is called the scope of the variable:

There is an exception to general variable scoping. Variables defined in a class at the class level can be accessed outside the class. We will discuss this further in the lesson on object variables.

Note the keyword final on the definition of myVar4. Final means the value of variable cannot be changed after it is initialized. The variable must be initialized where defined or in a class constructor (more later).

Such a variable can is used to store a constant value. Constants are values that do not change and are used in place of literally stating values in statements. This is very important when you reuse the same number many times in code, such as the diameter of a wheel. If you do not use a constant and the value changes, you will have to manually change each place the constant value is used. Watch this video for more about constants. Here are two code snippets showing how a constant is used:

If for some reason, we later realize that the conversion was wrong for some reason, we would have to go back and find every 12 and change it. As code becomes more complex, it is easy to miss an instance of using that number. If you use a constant, all changes at the definition of the constant will be carried through the rest of the code you use, making edits much simpler and less error-prone. Here is the same code as above, written with a constant for the number of inches in a foot.

In the Java world, variables are commonly called "members" or "fields".

Lesson navigation: