Database Reference
In-Depth Information
This matters, because you need to remember that you are never replicating an object when you pass
it around and set member variables equal to it, you are merely setting more pointers to the same
location. The only time an object is created is when you instantiate it by a call to new or some method
that generates a new object and returns it. We will also look at the clone method, which can be used to
return the address of a new copy of an object instead of returning the address of the original object.
The following examples might help. In the first, we just point another member at the existing object;
and in the second, a new object is created and we point the new member variable at it. When we set the
name member in the first example, we set the value in the originally existing object; and any other code
that points at the original object will see the changed name. In the second example, the change, so far, is
only seen locally.
ExampleObject newMember = existingObject;
newMember.name = “New Name”;
ExampleObject newMember = new ExampleObject();
newMember.name = “Another Name”;
Something different happens with primitive values. They are not Java objects and their value, rather
than their address, is passed in all references.
Members
Objects have both methods and members. Members in object-oriented programming are variables. By
variable, I mean a pointer. This variable might point to a primitive—like an int (integer)—or a Java
object (like a Date class). In Java, Strings are objects, not primitives. Plain old arrays are also objects
(with no methods).
Often, both methods and variables are referred to as “members” of a class, but I prefer to say
“methods and member variables. In this discussion, methods are not called members . And also, member
variables are correctly referred to as fields . I can't help throwing that in once in a while. If I used the
terms field s and methods I wouldn't need to say members ; but I like the word members and am in the
habit of saying it, so I will say members and methods .
In our sample code, MyApp2 has several members. The first two we see are named myArray and
myRef . Those are both class member variables, because they exist in the class, outside of any methods,
which you can see from the code (repeated in the following). Methods know about the class member
variables as you can see in the getArray() method, which returns the class member myArray . You can
also see that the setRef() method sets the value of the myRef class member to be equal to the parameter
that is passed to the method, useRef .
private ARRAY myArray = null;
static MyRef myRef;
public ARRAY getArray() {
return myArray;
}
void setRef( MyRef useRef ) {
myRef = useRef;
}
If you look for a minute at the main() method (repeated in the following), you will see that there are
three members declared: m , useRef and mA . These are method members, because they exist solely in the
method. Note that they can be handed around to be used elsewhere. For instance, the main() method
hands useRef to the method m.setRef() , which then sets the myRef class member (remember, we are
 
Search WWH ::




Custom Search