Java Tutorial #3 - Variables

Welcome to the Java Variables tutorial. I am Matthew General, I will be leading you through this tutorial on the way variables are made, what variables are, and what is really going on in both the code and the machine as well as some other java related subjects that are akin to variables.

Please follow along as more of a refresher, rather than relying on this tutorial to understand everything we have learned this semester regarding variables.

What is a variable?

The all important question, what is a variable? In order to understand what variables do, we have to understand what a variable is. A variable in the context of Java is not the same as in other aspects of life. In math, a variable acts to hold some value that needs to be found. In science, it is there to change and see what happens to an experiment. In Java, however, it is there to act as a nice name for us humans to use in order to find the value of an expression.

When I say find the value of an expression, I literally mean find it. Variables represent a way for the programmer to reference some memory address on the computer. Variables ease this process. The variable makes finding where the expression's value is stored much easier to remember for us.

Show that code!

First thing we need to do java-wise is create the variable. Let us do that now. We will go back to chapter 3 and look at the beginning of the semester.

chapter1.Terrarium terrarium1 = new chapter1.Terrarium();

A quick dissection of this follows:

<objectType> <variableIdentifier> = new <Object>();

Here it is important to realize that "=" does not stand for equals, in reality it stands for assignment. From left to right we have the objectType, this is the variable type. When we are dealing with objects we will use the object's class name, such is the case in chapter1.Terrarium. However it is important to realize that there are other types for variables depending on what the variable is to hold. Java is strict with types meaning that you have to declare the exact type a variable is to hold regarless of allocating excess memory space or not (Think back to the 'box' lecture Professor Decker used. A hat box for a hat, an elephant box for an elephant).

There is more than one way to initialize variables. Here is another example of how this can be done

chapter1.Terrarium terrarium1;
terrarium1 = new chapter1.Terrarium();

In this example we first set the variable type which allocates space for the object. This is done in the first line. The second line actually assigns some value to the variable by evaluating an expression.

More about variables! Who would'a thunk it

Local variable dependency relationship, it's a mouth full. Essentially what this is all about, is that variables can be used to store a reference to an object so that we can communicate with that object and do other things that we couldn't do before when we just called a simple new object. Local variable dependency occurs when you use a class to call on another class while refering to the second class using a variable. The code will make this more clear.

package localVars;
public class LocalClass {
     public LocalClass() {
         localVars.LocalClass2 localVariable = new localVars.LocalClass2();
    }
}

Parameters!

Another place you will find variables in is parameter lists. Parameter lists are found with methods. Variables in the parameter list allow the programmer to pass information into the method for the method to make use of. The variable still remains a reference but the method acts on the variable's referenced value and does whatever the method is programed to do with it. Lets see an example:

package param;
public class ExampleClass() {
     public ExampleClass() {
     }
     public void paramExamp(param2.Example2 exampleVar){
          new param3.Example3(exampleVar);
     }
}

The above example shows how to use a variable in a parameter list. The variable exampleVar is accepted by the method paramExamp. exampleVar must be an object of type param2.Example2 and will then be able to be used to create a new object from that variable in the method paramExamp. While the parameter, when called in the program, does not have to be called exampleVar or even have to be a variable, it does have to be of that specific type. Any time we use that object in the method we will call it by exampleVar, however.

Instance Variables

Finally, let us look at instance variables. Instance variables are variables that will be used through out a class, meaning that their life time is that of the object and their scope is that of the class they are declared in.

package foo;
public class Bar() {
     private lab3.BouncingBall _ball;
     public Bar(){
          _ball = new lab3.BouncingBall();
     }
}

Instance variables are always private so that they can only be accessed by the class that they are defined in. Naming conventions start instance variables with an underscore and the first word is lowercase followed by capitalized letters at the beggining of each word from then on. The variable is always initialized before any method is defined, therefore it is done in the class header. We generally set the value of an instance variable in the constructor of a class.

Copyright © Matthew General, 2008.