Topic 1.14 Notes – Calling Instance Methods
1. What Instance Methods Are
An instance method is a method that belongs to an object, not the class itself.
Inside a class, it’s written without the static keyword. That’s your first clue.
public class Counter {
private int count;
public void increment() {
count++;
}
public int getCount() {
return count;
}
}
Here:
increment()andgetCount()are instance methods- They operate on one specific object’s
count - Every
Counterobject has its owncount
If you create two objects:
Counter c1 = new Counter();
Counter c2 = new Counter();
Then:
c1.increment();
Only c1 changes. c2 is unaffected.
That’s the key idea:
Instance methods operate on the state (instance variables) of one specific object.
2. How to Call an Instance Method
You must have an object first. Then you use the dot operator (.).
General form:
objectReference.methodName(arguments);
What the Dot Operator Does
The dot connects:
- the object
- to its behavior
Here’s what that looks like visually when c1.increment() is called:

Calling an instance method on a specific object
When you write:
c1.increment();
You are telling that specific object to run its method.
Methods That Return a Value
If the method has a return type (like int, String, etc.), the call produces a value.
int value = c1.getCount();
The method call can also appear inside expressions:
System.out.println(c1.getCount());
With built-in classes:
String word = "computer";
int len = word.length(); // returns 8
String part = word.substring(0, 4); // "comp"
You can even chain calls:
word.substring(0, 4).length();
Each method must return something that supports the next method.
Void Methods
If the method is void, it performs an action but does not return a value.
c1.increment();
You cannot do:
int x = c1.increment(); // ERROR
A common quiz mistake is trying to store the result of a void method.
3. Instance vs Static Methods
This shows up constantly on MCQs.
Instance Methods
- Called on an object
- Use a variable before the dot
word.length();
list.add("A");
Static Methods
- Belong to the class
- Called using the class name
Math.sqrt(25);
Here’s the comparison:
| Instance Method | Static Method |
|---|---|
| Called on object | Called on class |
| Uses object reference | Uses class name |
| Can access instance variables | Cannot access instance variables directly |
Example: text.length() |
Example: Math.random() |
Recognition trick:
variable.method()→ instanceClassName.method()→ static
If you see:
String.length(); // compile error
That’s wrong because length() needs an object.
4. Null References and NullPointerException
An object variable can either:
- Refer to an object
- Or be
null
Counter c = null;
This compiles. But this crashes:
c.increment(); // NullPointerException at runtime
What’s happening?
The variable exists. But it does not point to an object in memory. Java tries to follow the dot. There’s nothing there.
Picture c as a variable that exists, but doesn’t point to any object:

Null reference causing a NullPointerException
This error is extremely common on tests because the code looks fine syntactically.
Typical causes:
- Forgot
new - Method returned
null - Variable explicitly set to
null
Safe pattern:
if (c != null) {
c.increment();
}
When tracing code on a quiz, always ask: Does this reference definitely point to an object right now?
If not, expect a runtime error.