Calling
Instance Methods
Learn how objects come alive: create objects, call their methods, and understand the dot operator that makes it all work.
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).
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.
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.
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.
objectReference.methodName(arguments);
Void vs. Return-Value Methods
Instance methods come in two flavors, depending on whether they produce a result or simply perform an action.
list.add("item");
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.
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.
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
Example 1: String Methods
// 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).
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
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.
int len = s.length(); // NullPointerException!
Common Mistakes to Avoid
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);