<< Chapter < Page | Chapter >> Page > |
The last two statements in Listing 1 access a public instance variable belonging to the object. The first of the two statements assigns the stringvalue "Quit" to the variable. The second statement retrieves and displays that value.
Note the syntax of the first of these two statements. The value of the reference variable named obj is joined to the name of the object's public instance variable. (The name of the instance variable is text .) The assignment operator is used to store a string value in that instance variable.
You can think of this operation as involving the following steps:
The last statement does essentially the same thing in reverse.
This produces the first line of output shown in Figure 1 .
The class definition for the class named TargetClass begins in Listing 2 .
Listing 2 . Beginning of the class named TargetClass.
public class TargetClass{public string text;
The code in the class begins by declaring a public instance variable. This is considered to be bad programming practice in most quarters unless the publicvariable is actually a constant, (which it is not).
This is the variable that is accessed by the last two statements in Listing 1 .
In most cases, instance variables should be declared private and should be made accessible through public accessor methods or public set and get methods. Youwill see examples of public access, set, and get methods later.
I will generally refer to three kinds of variables:
Any of these can be further qualified as follows:
An instance variable belongs to a specific object. The lifetime of an instance variable is the same as the lifetime of the object to which it belongs.The scope of an instance variable depends on its access modifier such as public , private , or protected .
Notification Switch
Would you like to follow the 'Xna game studio' conversation and receive update notifications?