Unit 1.14
Calling Instance Methods

AP Computer Science A

Using Objects and Methods

Learning Objectives

By the end of this lesson, you will be able to:

  • Understand what instance methods are and how they work
  • Use the dot operator to call instance methods on objects
  • Develop code that calls instance methods correctly
  • Determine the results of method calls
  • Recognize and avoid NullPointerException errors

What Are Instance Methods?

Instance methods are actions or behaviors that objects can perform.

  • Instance methods belong to a specific object of a class
  • They are called ON objects, not on the class itself
  • Each object has access to the instance methods defined in its class

Real-World Analogy:

Think of a car object. Instance methods would be actions like:

  • startEngine()
  • accelerate()
  • brake()

Each individual car can perform these actions.

The Dot Operator

To call an instance method, we use the dot operator (.)

Syntax:

objectName.methodName(parameters);

Components:

  • objectName - the specific object you're working with
  • . - the dot operator (connects object to method)
  • methodName - the name of the method to call
  • (parameters) - any values the method needs

String Method Examples

Let's look at calling instance methods on String objects:

String greeting = "Hello World"; // Calling length() method int len = greeting.length(); // len is now 11 // Calling substring() method String part = greeting.substring(0, 5); // part is now "Hello" // Calling indexOf() method int position = greeting.indexOf("World"); // position is now 6

Each method is called ON the greeting object using the dot operator.

Methods with Parameters

Many instance methods require parameters (inputs):

String text = "Computer Science"; // substring with TWO parameters String sub1 = text.substring(0, 8); // Returns "Computer" // substring with ONE parameter String sub2 = text.substring(9); // Returns "Science" // indexOf with a String parameter int index = text.indexOf("Science"); // Returns 9

Important: You must provide the correct number and type of parameters!

Methods that Return Values

Instance methods can return a value that you can use:

String word = "Java"; // Store the return value in a variable int length = word.length(); System.out.println(length); // Prints: 4 // Use the return value directly System.out.println(word.substring(0, 2)); // Prints: Ja // Use in expressions if (word.length() > 3) { System.out.println("Long word"); }

NullPointerException

CRITICAL CONCEPT

A method call on a null reference will result in a NullPointerException.

What is null?

null means the variable doesn't reference any object.

String text = null; // text refers to nothing // This will cause a NullPointerException! int len = text.length();

Why? You can't call a method on something that doesn't exist!

Avoiding NullPointerException

Check for null before calling methods:

String text = null; // UNSAFE - will crash if text is null int len = text.length(); // SAFE - check first if (text != null) { int len = text.length(); System.out.println(len); } else { System.out.println("Text is null"); }

Best Practice: Always consider whether an object might be null before calling methods on it.

Summary

Key Takeaways:

  • Instance methods are called on objects of a class
  • Use the dot operator (.) to call instance methods
  • Syntax: objectName.methodName(parameters)
  • Methods can take parameters and return values
  • Calling a method on null causes a NullPointerException
  • Always check for null when necessary

Remember for the AP Exam:

You must be able to determine the result of method calls and write code that correctly calls instance methods on objects.

1 / ?