Unit 1.14: Calling Instance Methods¶
Scope: CS Awesome 2, Section 1.14
Learning Goals¶
By the end of this lesson, you should be able to:
- Distinguish class methods from instance methods.
- Call an instance method through an object reference.
- Match method arguments to a signature.
- Use values returned by non-void instance methods.
- Explain why a call through
nullfails.
Class Methods vs. Instance Methods¶
A class method is associated with a class and marked static.
double root = Math.sqrt(25);
An instance method describes behavior performed by or on one object.
String label = "north";
int size = label.length();
| Method kind | Typical call |
|---|---|
| class method | ClassName.method(arguments) |
| instance method | objectReference.method(arguments) |
Calling an Instance Method¶
The dot operator connects an object reference to its method.
objectReference.method(arguments);
For a hypothetical Drone API:
void move(int meters)
void turnLeft()
double getBatteryLevel()
Calls on an object referenced by surveyDrone:
surveyDrone.move(20);
surveyDrone.turnLeft();
double battery = surveyDrone.getBatteryLevel();
Each call targets that specific object.
Method Signatures and Calls¶
Given the signature:
move(int)
the call needs exactly one compatible int argument.
surveyDrone.move(20); // valid
surveyDrone.move(); // missing argument
surveyDrone.move(2.5); // double cannot narrow to int
Parentheses are required even when a method takes no arguments:
surveyDrone.turnLeft();
Methods That Change State¶
Void instance methods often change an object's attributes.
surveyDrone.move(20);
surveyDrone.turnLeft();
After these calls, the same drone object may have a new location and direction. Another drone object is unaffected.
Methods That Return Values¶
A non-void instance method returns information to the caller.
double battery = surveyDrone.getBatteryLevel();
System.out.println(surveyDrone.getBatteryLevel());
Use the returned value in a compatible context. Do not try to store a void call.
double battery = surveyDrone.move(20); // invalid if move is void
Flow of Control¶
When an instance method is called:
- the object reference identifies the receiving object;
- argument values are copied into parameters;
- control moves to the method body;
- the method may inspect or change the object's state;
- a returned value is sent back if the method is non-void;
- execution resumes after the call.
The null Reference¶
String message = null;
int size = message.length();
The compiler knows that message has type String, so the code can compile. At run time there is no object to receive length(), causing a NullPointerException.
This is the key precondition for every instance call:
The object reference used for the call must not be null.
Chaining and Returned Objects¶
If a method returns an object, another instance method may be called on that result.
String city = " Seoul ";
String cleaned = city.trim().toUpperCase();
Read a chain from left to right: trim() returns a String, then toUpperCase() is called on that returned object. Use chains only when each return type supports the next method.
Practice Missions¶
Mission 1: Class or Instance Call?¶
Classify each call and identify the receiver:
Math.abs(-8)
route.length()
scanner.nextInt()
Math.random()
drone.turnLeft()
Mission 2: Drone API Calls¶
Using the Drone API above, write code that:
- moves a drone 50 meters;
- turns it left twice;
- stores and prints its battery level.
Mission 3: Diagnose the Calls¶
Explain each error based on the API signatures.
int battery = surveyDrone.getBatteryLevel();
surveyDrone.turnLeft(90);
double moved = surveyDrone.move(10);
surveyDrone.move(4.5);
Mission 4: Null Failure Timeline¶
Explain why this code compiles but fails at run time. Name the exception and the violated precondition.
String status = null;
System.out.println(status.length());
Mission 5: Trace Two Objects¶
Two Drone objects begin at the same location. Calls move only the first drone and turn only the second. Draw separate state boxes and explain why the methods do not affect both objects.
Key Summary¶
| Concept | Core idea |
|---|---|
| Instance method | Behavior called on a specific object. |
| Dot operator | Connects a receiver to a method. |
| Arguments | Must match parameter number, order, and type. |
| Void instance method | Often changes object state. |
| Non-void instance method | Returns a value to use. |
null call |
Causes NullPointerException at run time. |
Source scope: CS Awesome 2, Unit 1.14