TOPIC 1.13 · STUDY
Object Creation and Storage (Instantiation)
A constructor signature is the class name plus the ordered parameter types. new picks the matching overload, creates the object, and a typed variable stores a reference to it. After construction, getters and other methods use that object's stored state.
1.13.A
Constructor signatures and overload selection
In this topic you use a supplied class. You do not write the class yourself. You call its constructors with new and later call its methods. A constructor signature is the constructor name (the same as the class name) together with the ordered list of parameter types. The signature omits public, void, new, and the parameter names.
class Bin {
private int count;
private String tag;
public Bin() {
count = 0;
tag = "empty";
}
public Bin(int n) {
count = n;
tag = "bulk";
}
public Bin(int n, String label) {
count = n;
tag = label;
}
public int getCount() {
return count;
}
public String getTag() {
return tag;
}
}
That supplied Bin class has three constructors. Their signatures are Bin(), Bin(int), and Bin(int, String). When you write new Bin(12), Java selects Bin(int) because the call has one int argument. new Bin("pine") does not match any of those signatures: there is no Bin(String) overload, so the call does not compile. Matching is by type and order, not by hoping Java will convert a String into an int.
Each call is matched only to a constructor whose parameter types line up in order.
Key points
- A constructor signature is the class name plus the ordered parameter types.
- Signatures omit public, void, new, and parameter names.
- new Bin(12) selects Bin(int); a different argument type selects a different overload, or none.
- If no supplied constructor matches the argument types, the call does not compile.
1.13.B
Creating an object and storing a reference
The expression new Bin(12) creates a Bin object in memory and runs the matching constructor. The variable on the left does not hold the object itself. It holds a reference: a value that points to that object. The variable's declared type must be Bin (or a compatible reference type). An int or boolean variable cannot store a Bin reference.
Bin shelf = new Bin(12);
Bin spare = null;
After the first line, shelf refers to the newly constructed Bin. A reference can also refer to no object. Assigning null means the variable is still typed as Bin, but it currently names nothing. spare = null is valid. int spare = null is not, because int is not a reference type.
new builds the object. shelf stores a reference that points to it, not a copy of the fields.
Key points
- new creates the object and runs the selected constructor.
- The variable stores a reference to that object.
- The variable's type must be the class (or a compatible reference type), not int or boolean.
- null is a valid reference value that currently refers to no object.
new creates the object; the variable only stores a reference to it.
1.13.C
Using the constructed object's state
After construction, the object's fields hold the values the constructor stored from the arguments (or from defaults in a no-arg constructor). A getter returns that stored state for the object you call it on. Calling a method always uses one specific object: the one the receiver reference points to.
Bin left = new Bin(12);
Bin right = new Bin(4, "pine");
System.out.print(left.getCount() + "," + right.getTag());
left was built with Bin(int), so getCount returns 12 and getTag would return "bulk". right was built with Bin(int, String), so getCount returns 4 and getTag returns "pine". Changing a local int you passed in earlier does not rewrite an object that already finished construction. Each call such as left.getCount() reads left's state, not right's.
Each reference points to its own object. A method call uses the fields of that object only.
Key points
- The constructor stores state from its arguments into the new object.
- A getter returns the state that object currently holds.
- A method call uses the specific object the receiver reference refers to.
- Two objects of the same class can hold different field values at the same time.