- Working with Objects
- Accessing and Setting Class and Instance Variables
- Calling Methods
- References to Objects
- Casting and Converting Objects and Primitive Types
- Comparing Object Values and Classes
- Summary
- Q&A
- Quiz
- Exercises
Accessing and Setting Class and Instance Variables
At this point, you could create your own object with class and instance variables defined in it, but how do you work with those variables? Easy! Class and instance variables are used in largely the same manner as the local variables you learned about yesterday. You can use them in expressions, assign values to them in statements, and the like. You just refer to them slightly differently from how you refer to regular variables in your code.
Getting Values
To get to the value of an instance variable, you use dot notation, with which an instance or class variable name has two parts: a reference to an object or class on the left side of the dot and a variable on the right side of the dot.
Dot notation is a way to refer to an object's instance variables and methods using a dot (.) operator.
For example, if you have an object named myCustomer, and that object has a variable called orderTotal, you refer to that variable's value like this:
myCustomer.orderTotal;
This form of accessing variables is an expression (that is, it returns a value), and both sides of the dot are also expressions. That means you can nest instance variable access. If the orderTotal instance variable itself holds an object, and that object has its own instance variable called layaway, you could refer to it like this:
myCustomer.orderTotal.layaway;
Dot expressions are evaluated from left to right, so you start with myCustomer's variable orderTotal, which points to another object with the variable layaway. You end up with the value of that layaway variable.
Changing Values
Assigning a value to that variable is equally easy; just tack on an assignment operator to the right side of the expression:
myCustomer.orderTotal.layaway = true;
This example sets the value of the layaway variable to true.
Listing 3.2 is an example of a program that tests and modifies the instance variables in a Point object. Point, a part of the java.awt package, refers to a coordinate point with x and y values.
LISTING 3.2 The Full Text of SetPoints.java
1: import java.awt.Point; 2: 3: class SetPoints { 4: 5: public static void main(String[] arguments) { 6: Point location = new Point(4, 13); 7: 8: System.out.println("Starting location:"); 9: System.out.println("X equals " + location.x); 10: System.out.println("Y equals " + location.y); 11: 12: System.out.println("\nMoving to (7, 6)"); 13: location.x = 7; 14: location.y = 6; 15: 16: System.out.println("\nEnding location:"); 17: System.out.println("X equals " + location.x); 18: System.out.println("Y equals " + location.y); 19: } 20: }
When you run this application, the output should be the following:
Starting location: X equals 4 Y equals 13 Moving to (7, 6) Ending location: X equals 7 Y equals 6
In this example, you first create an instance of Point where x equals 4 and y equals 13 (line 6). Lines 9 and 10 display these individual values using dot notation. Lines 13 and 14 change the values of x to 7 and y to 6, respectively. Finally, lines 17 and 18 display the values of x and y again to show how they have changed.
Class Variables
Class variables, as you have learned, are variables that are defined and stored in the class itself. Their values apply to the class and all its instances.
With instance variables, each new instance of the class gets a new copy of the instance variables that the class defines. Each instance then can change the values of those instance variables without affecting any other instances. With class variables, only one copy of that variable exists. Changing the value of that variable changes it for all instances of that class.
You define class variables by including the static keyword before the variable itself. For example, consider the following partial class definition:
class FamilyMember { static String surname = "Mendoza"; String name; int age; }
Each instance of the class FamilyMember has its own values for name and age. The class variable surname, however, has only one value for all family members: "Mendoza". Change the value of surname, and all instances of FamilyMember are affected.
NOTE
Calling these static variables refers to one of the meanings of the word static: fixed in one place. If a class has a static variable, every object of that class has the same value for that variable.
To access class variables, you use the same dot notation as with instance variables. To retrieve 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 lines of output in this example display 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 its 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 makes strange results easier to debug.