콘텐츠로 이동

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:

  1. Distinguish class methods from instance methods.
  2. Call an instance method through an object reference.
  3. Match method arguments to a signature.
  4. Use values returned by non-void instance methods.
  5. Explain why a call through null fails.

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:

  1. the object reference identifies the receiving object;
  2. argument values are copied into parameters;
  3. control moves to the method body;
  4. the method may inspect or change the object's state;
  5. a returned value is sent back if the method is non-void;
  6. 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:

  1. moves a drone 50 meters;
  2. turns it left twice;
  3. 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