All the rar files have the same 
password : http://learning4you.blogspot.com/

Sunday, November 9, 2008

Accessing and Setting Class and Instance Variables

Accessing and Setting Class and Instance Variables

Now you have your very own object, and that object may have class or instance variables defined in it. How do you work with those variables? Easy! Class and instance variables behave in exactly the same ways as the local variables you learned about yesterday; you just refer to them slightly differently than you do regular variables in your code.

Getting Values

To get to the value of an instance variable, you use an expression in what's called dot notation. With dot notation, the reference to an instance or class variable has two parts: the object on the left side of the dot and the variable on the right side of the dot.
New Term
Dot notation is an expression used to get at instance variables and methods inside a given object.

For example, if you have an object assigned to the variable myObject, and that object has a variable called var, you refer to that variable's value like this:

myObject.var;
This form for accessing variables is an expression (it returns a value), and both sides of the dot can also be expressions. This means that you can nest instance variable access. If that var instance variable itself holds an object and that object has its own instance variable called state, you could refer to it like this:
myObject.var.state;
Dot expressions are evaluated left to right, so you start with myObject's variable var, which points to another object with the variable state. You end up with the value of that state variable after the entire expression is done evaluating.

Changing Values

Assigning a value to that variable is equally easy-just tack an assignment operator on the right side of the expression:
myObject.var.state = true;
Listing 4.2 is an example of a program that tests and modifies the instance variables in a Point object. Point is part of the java.awt package and refers to a coordinate point with an x and a y value.
Listing 4.2. The TestPoint Class.
 1: import java.awt.Point;
2:
3: class TestPoint {
4: public static void main(String args[]) {
5: Point thePoint = new Point(10,10);
6:
7: System.out.println("X is " + thePoint.x);
8: System.out.println("Y is " + thePoint.y);
9:
10: System.out.println("Setting X to 5.");
11: thePoint.x = 5;
12: System.out.println("Setting Y to 15.");
13: thePoint.y = 15;
14:
15: System.out.println("X is " + thePoint.x);
16: System.out.println("Y is " + thePoint.y);
17:
18: }
19:}

X is 10
Y is 10
Setting X to 5.
Setting Y to 15.
X is 5
Y is 15

Analysis
In this example, you first create an instance of Point where X and Y are both 10 (line 6). Lines 8 and 9 print out those individual values, and you can see dot notation at work there. Lines 11 through 14 change the values of those variables to 5 and 15, respectively. Finally, lines 16 and 17 print out the values of X and Y again to show how they've changed.

Class Variables

Class variables, as you've already learned, are variables that are defined and stored in the class itself. Their values, therefore, apply to the class and to all its instances.

With instance variables, each new instance of the class gets a new copy of the instance variables that class defines. Each instance can then change the values of those instance variables without affecting any other instances. With class variables, there is only one copy of that variable. Every instance of the class has access to that variable, but there is only one value. Changing the value of that variable changes it for all the instances of that class.

You define class variables by including the static keyword before the variable itself., "Creating Classes and Applications in Java." For example, take the following partial class definition:

class FamilyMember {
static String surname = "Johnson";
String name;
int age;
...
}
Instances of the class FamilyMember each have their own values for name and age. But the class variable surname has only one value for all family members. Change surname, and all the instances of FamilyMember are affected.

To access class variables, you use the same dot notation as you do with instance variables. To get or change the value of the class variable, you can use either the instance or the name of the class on the left side of the dot. Both of the lines of output in this example print the same value:

FamilyMember dad = new FamilyMember();
System.out.println("Family's surname is: " + dad.surname);
System.out.println("Family's surname is: " + FamilyMember.surname);
Because you can use an instance to change the value of a class variable, it's easy to become confused about class variables and where their values are coming from (remember that the value of a class variable affects all the instances). For this reason, it's a good idea to use the name of the class when you refer to a class variable-it makes your code easier to read and strange results easier to debug.

0 comments:

Related Posts with Thumbnails