01 / 20

Calling
Instance Methods

Learn how objects come alive: create objects, call their methods, and understand the dot operator that makes it all work.

AP Computer Science A

What Is an Object?

An object is a specific instance of a class. Think of a class as a blueprint and an object as an actual building constructed from that blueprint. Each object has its own state (data stored in instance variables) and behavior (actions defined by methods).

String (Class / Blueprint) length() substring() indexOf() str1 "Hello" str2 "World"

What Is an Instance Method?

An instance method is a method that belongs to a specific object. It is called on an object and can access or modify that object's state. Unlike static methods, instance methods require an object to exist before they can be invoked.

Instance Method
Called on an object using the dot operator. Can read/modify the object's instance variables.
Static Method
Called on the class itself. Does not require an object. Cannot access instance variables directly.

Why Do Instance Methods Matter?

Instance methods let objects do things. Without them, objects would just be data containers. Methods give objects behavior: a String can report its length, a Scanner can read input, and an ArrayList can add elements.

1
Encapsulation: Methods bundle behavior with data, hiding internal implementation details.
2
Reusability: Write a method once in a class: every object of that class can use it.
3
Abstraction: You call str.length() without knowing how lengths are stored internally.

The Dot Operator Syntax

Every instance method call follows the same fundamental pattern. The dot operator (.) connects an object reference to the method you want to invoke.

// General syntax
objectReference.methodName(arguments);
objectReference who receives the call . methodName what to do (arguments) data to pass in

Void vs. Return-Value Methods

Instance methods come in two flavors, depending on whether they produce a result or simply perform an action.

void Methods
Perform an action but return nothing. Called as standalone statements.

list.add("item");
Return-Value Methods
Perform an action and return a value. The return value must be used or stored.

int n = str.length();

Method Signatures & Parameters

A method signature includes the method name and its parameter list. The arguments you pass when calling the method must match the parameter types in the signature.

// Method signature: substring(int, int)
String result = str.substring(0, 5);

// Method signature: indexOf(String)
int pos = str.indexOf("lo");

// Method signature: length() (no parameters)
int len = str.length();

Creating Objects with new

Before calling instance methods, you need an object. The new keyword invokes a constructor to create one. The constructor initializes the object's state.

// Creating a String object
String greeting = new String("Hello");

// Creating a Scanner object
Scanner sc = new Scanner(System.in);

// Now we can call instance methods
int len = greeting.length(); // returns 5

How to Call an Instance Method

1
Identify the class that defines the method you need (e.g., String, ArrayList).
2
Create an object of that class (or use an existing reference variable).
3
Use the dot operator to connect the object to the method name.
4
Pass arguments matching the method's parameter types (if any).
5
Use the return value if the method returns one: store it, print it, or pass it to another method.

Example 1: String Methods

String word = "Computer";

// length() : returns int
int len = word.length(); // 8

// substring(int, int) : returns String
String part = word.substring(0, 3); // "Com"

// indexOf(String) : returns int
int idx = word.indexOf("put"); // 3

// toUpperCase() : returns String
String upper = word.toUpperCase(); // "COMPUTER"

Example 2: Chaining & Nesting Calls

Because return-value methods produce a result, you can chain them (call a method on the return value) or nest them (use a method call as an argument).

// Chaining: result of substring() is a String
int n = "Hello World".substring(6).length();
// "World".length() → 5

// Nesting: indexOf() return used in substring()
String s = "abcdef";
String tail = s.substring(s.indexOf("d"));
// s.indexOf("d") → 3, then "abcdef".substring(3) → "def"

Example 3: Methods on Different Classes

import java.util.ArrayList;

ArrayList<String> names = new ArrayList<String>();

// void method: add(String)
names.add("Alice");
names.add("Bob");

// return-value method: get(int) → String
String first = names.get(0); // "Alice"

// return-value method: size() → int
int count = names.size(); // 2

The NullPointerException

If you try to call a method on a reference that is null (it doesn't point to any object), Java throws a NullPointerException at runtime.

String s = null;
int len = s.length(); // NullPointerException!
s null ? No object .length() crashes Fix: initialize s = "Hello";

Common Mistakes to Avoid

Forgetting Parentheses
Writing str.length instead of str.length(). Without (), Java doesn't see a method call.
Ignoring Return Value
Calling str.toUpperCase(); without storing the result. Strings are immutable: the original doesn't change.
Calling on null
Invoking a method on a variable that was never assigned an object. This causes a NullPointerException at runtime.

Summary Comparison

Instance Method Static Method
Called on An object The class name
Syntax obj.method() Class.method()
Requires object? Yes No
Access instance vars? Yes No
Example str.length() Math.abs(-5)

Question 1 of 5

What is the output of the following code?

String s = "JavaRocks"; System.out.println(s.substring(4));

Question 2 of 5

Which of the following correctly calls an instance method?

Question 3 of 5

What value does s.indexOf("fun") return if s = "Java is fun!"?

Question 4 of 5

What happens when this code runs?

String s = null; s.length();

Question 5 of 5

What is the value of result after this code executes?

String s = "abcdef"; String result = s.substring(s.indexOf("c"), 5);