Declaring an Instance of a Class


An instance of a class must be declared before attributes and methods of the class can be used in a program. That is, you must cut out a cookie in the dough using the cookie cutter before you can bake the cookie. The cookie cutter is the class definition, and the cookie cutout of the dough is an instance. Remember that a class definition (cookie cutter) only describes attributes and methods (that is, the legs and tail of the dog cookie) of the class.

Let s assume that a class definition has two attributes: studentNumber and courseNumber. In this example, both are integers that require 2 bytes of memory. Figure 2-3 shows how the instance of this class appears in memory. The class definition doesn t reserve any memory but simply defines what memory must be reserved when an instance is created.

click to expand
Figure 2-3: This figure shows how an instance of the RegistrationForm class reserves memory.

An instance of a class is created in a declaration statement, as shown in this Java example:

 RegistrationForm myRegistrationForm = new RegistrationForm(); 

This statement looks imposing , so let s take apart the statement and see how it works. We ll begin with the right side of the assignment operator:

 new RegistrationForm() 

This tells the computer to dynamically reserve a block of memory the size of the RegistrationForm class. Dynamically means that memory is reserved when the computer executes the program, which is called runtime. Memory for most variables and arrays is reserved at compile time, using the following declaration statement:

 int grade; 

The class size is the size of all the class s attributes. Reserving memory dynamically occurs when the program executes rather than when the program is compiled.

Once the computer reserves the block of memory, it returns a pointer to the first memory address of the block. A pointer is like someone pointing to your instructor s office.

Let s move on to the second part of the statement:

 RegistrationForm myRegistrationForm 

This portion of the statement declares a reference to an instance of the RegistrationForm class called myRegistrationForm . This is a mouthful to say, so let dissect it to get a better understanding at what it means.

A reference is something that refers you to something else. In this case, the reference is going to refer you to an instance of the RegistrationForm class.

The name of the reference in the previous example is myRegistrationForm . This is like saying myRootCanalSpecialist . You use the name of the reference whenever you want to refer to the instance of RegistrationForm .

A reference is not an instance of a class. It is only a symbol that refers to an instance. The final part of the declaration statement assigns the pointer to the instance to the reference using the assignment operator. You then use the reference ( myRegistrationForm ) in the program whenever you want to refer to the instance of the class.

Initializing an Instance Variable

Initialization is the process of assigning a value to a variable when the variable is declared. Programmers do this to prevent a possible error if the variable is used without being assigned a value. Think of this as telling the computer to give you what is in a carton, but you ve never stored anything in the carton, so the computer complains.

As you probably remember from your programming course, a variable is initialized by assigning a value to the variable when it is declared, such as in the following statement:

 String status = "No Change"; 

In C++, an instance variable cannot be initialized this way, but in Java it can. For C++, an instance variable must be initialized using a special member method called a constructor that s automatically called when an instance of a class is declared. The constructor has the same name as the class. Constructors exist in both C++ and Java, as well as in other OOP languages.

How to define a constructor to initialize an instance variable is shown next . This listing defines the RegistrationForm class that you learned about. The RegistrationForm class declares an attribute called status and defines two methods. The first method is RegistrationForm , which is the constructor because it has the same name as the class. The other method is dropCourse , which you saw earlier.

The constructor assigns the message Course Not Changed to the instance variable status . This is the default status for each instance of the class. The value of the status instance variable is changed from the default value to the message Course Dropped by the dropCourse method.

 class MyJavaApplication { 
public static void main (String args[]) {
RegistrationForm regForm = new RegistrationForm();
regForm.dropCourse(CS102,1234);
System.out.println("Status: " + regForm.status);
}
}
class RegistrationForm {
String status;

void RegistrationForm () {
status = "Course Not Changed.";
}
void dropCourse(int courseNumber, int studentNumber)
{status = "Course: "+ courseNumber + " is dropped for
student: " + studentNumber;
}
}

Accessing an Instance Variable

An instance variable is accessed in a program basically the same way you call a method member. First, create an instance of the class and then use the name of the instance and the dot operator to reference the instance variable. Here s how to access the status instance variable from within your program:

 instanceName.instanceVariableName 

An instance of the RegistrationForm class, called regForm , is declared in the main method. Next, the dropCourse method is called as described in the Calling a Method section of this chapter. After the dropCourse method executes, the value of the status instance value is printed on the screen by directly accessing the instance variable.

Assigning a Value to an Instance Variable from Your Program

The value of an instance variable can be changed using an assignment statement in your program. This process is nearly identical to how a value is assigned to a variable, except that you use the name of the instance to reference the instance variable, as shown here:

 regForm.status = "No Status"; 

Any change in the value of the instance variable is accessible to the member methods. How this is done is shown below. After an instance of the RegistrationForm class is declared in the main method, an assignment statement changes the default value of the instance variable status to the message No Status.

The next statement calls the displayStatus method that is defined in the RegistrationForm class. This method displays the value of the status instance variable on the screen. The message No Status appears when this method is called.

The dropCourse method then changes the value of the status instance variable to Course Dropped , which is then displayed by the program.

Caution  

Many programmers frown on letting a program directly change the value of an instance value because this practice is fraught with the danger that the data will be corrupted. Programmers prefer that only member methods change an instance variable s value. That way, safeguards can be placed in the member method to prevent data from being corrupted. A program then calls the member method whenever it needs to change the value of an instance variable.

 class MyJavaApplication { 
public static void main (String args[]) {
RegistrationForm regForm = new RegistrationForm();
regForm.status = "No Status";
regForm.displayStatus();
regForm.dropCourse(CS102,1234);
System.out.println("Status: " + regForm.status);
}
}
class RegistrationForm {
String status;
void RegistrationForm () {
status = "Course Not Changed.";
}
void dropCourse(int courseNumber, int studentNumber) {
status = "Course: "+ courseNumber + " is dropped for
student: " + studentNumber;

}
void displayStatus();
System.out.println("Status: " + status);
}
}



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net