Dove's LibraryDove's Library

1.14 - Calling Instance Methods

Instance methods are called on actual objects of a class, using the dot operator with the object’s name.
Dog myDog = new Dog("Rex"); myDog.bark(); // calling an instance method on the object
You’ve already seen this with strings. length is an instance method, so you call it on a specific String object:
String school = "Dove"; int len = school.length(); // 4
Each object has its own attribute values, so calling the same method on two different objects can give different results. Say you’re given this class:
public class Counter { private int count; public Counter() { // starts count at 0 count = 0; } public void increment() { // adds 1 to count count++; } public int getCount() { // returns count return count; } }
Counter clicks = new Counter(); Counter taps = new Counter(); clicks.increment(); clicks.increment(); taps.increment(); System.out.println(clicks.getCount()); // 2 System.out.println(taps.getCount()); // 1
increment is a void method, so it’s called on its own line. getCount is non-void, so its return value gets used, here by printing it.
Compare this to class methods: an instance method needs an object in front of the dot (clicks.increment()), while a class method uses the class name (Math.sqrt(9.0)). Calling an instance method with just the class name, like Counter.increment(), won’t compile.
Careful: calling a method on a null reference throws a NullPointerException, since there’s no actual object there to call the method on.
Dog noDog = null; noDog.bark(); // NullPointerException, nothing to call bark() on
This is a run-time error, not a compile error. The code compiles fine because noDog is a Dog variable, but when it runs there’s no object to call bark on, so the program crashes.

AP® is a registered trademark of the College Board, which is not affiliated with Dove's Library and was not involved in the production of, and does not endorse, this website.

Providing free, high-quality AP notes to help students succeed, earn college credit, and reach their educational goals, ensuring everyone has access to the resources they need.

© 2026 Standard Learning Group, Inc., which operates Dove's Library. All rights reserved.