CSE 115 Tutorial 3 Variables

Designer: Yingrui Liu

 
Home
Declaration
Assignment
Scope & lifetime
Relationship & Methods

In java, we use a variable to associate a name with an object reference.
There are many different types of variables and so far we have learned three types:

Local variables, Parameters, and Instance variables.

Local variables

Being declared and exist within a method.
Used in the "Local Variable Dependency Relationship."

Instance variables

Being declared within a class and outside the methods which is in the class.
Variable that associated with an instance of a class.
Used in the "Composition Relationship" and the "Association Relationship."
Also, we will talk about the two methods, Mutator Methods and Accessor Methods in which instance variables are used.

Parameters

Parameters are in the formal parameter-list of the method header within the parenthesis "()".
Used to describe values which need to be given to method for it to do its job.
A parameter can be a local variable or an instance variable.

Here is an example of a part of java program from lab4.
It is the CreateElephantListener.java which is modified to create an image of elephants. In the Declaration and the Assignment parts of the tutorial we will use examples from this program.

//The First program: CreateElephantListener.java

package lab4;

  import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import lab4lib.Elephant;
 import lab4lib.WindowWithButtons;

public class CreateElephantListener implements ActionListener {

     private WindowWithButtons _windowWithButtons;
 
public CreateElephantListener(WindowWithButtons windowWithButtons) {

       _windowWithButtons = windowWithButtons;
}

public void actionPerformed(ActionEvent ag0) {
       Elephant elephant;
       elephant = new Elephant();
       _windowWithButtons.add(elephant);
}

}


back to top