Instance methods are behaviors that belong to specific objects, and you call them with the dot operator using the pattern objectReference.methodName(). Each object runs the method using its own data, so two objects of the same class can give different results. For AP Computer Science A, check that the reference points to an object before tracing what the method call does.
How Do You Call an Instance Method in Java?
Call an instance method with an object reference, a dot, the method name, and parentheses: objectReference.methodName(). The reference must point to an actual object; if it is null, the method call compiles but throws a NullPointerException when that line runs.

Why This Matters for the AP Computer Science A Exam
This topic shows up constantly in AP Computer Science A because almost every program uses objects. On the multiple-choice section, you will trace code that creates objects and calls instance methods, then predict the output or spot why a line will not compile or will crash. Knowing how the dot operator works, and when a null reference triggers a NullPointerException, helps you avoid common traps.
For free-response code writing, you call instance methods on objects you are given, including String objects and other provided classes. Being able to read a class and call its methods correctly is a skill you will use throughout the rest of the course, since later units build directly on this.
Key Takeaways
- Instance methods are called on objects, using the dot operator:
objectReference.methodName(). - Each object runs an instance method using its own state, so the same method can produce different results on different objects.
- Calling any method on a
nullreference throws aNullPointerException, which is a run-time error, not a compile error. - Instance methods differ from class (static) methods, which are called on the class name instead of an object.
- Instance methods can be void or can return a value, just like other methods.
- Always make sure a reference actually points to an object (was assigned with
newor set to an existing object) before calling a method on it.
Key Concepts
Instance Methods Belong to Objects
An instance method is a behavior that belongs to a specific object, not to the class itself. You need an actual object to call one. They are called "instance" methods because they work with instances (objects) of a class.
When you create an object using new, you can call any instance method defined in its class on that object. These methods can use that specific object's data, so each object can behave differently even though they share the same method code.
The Dot Operator
The dot operator (.) is how you call an object's instance methods. The pattern is always objectReference.methodName(). The object reference tells Java which object should run the method, and the method name tells it what to do.
This creates a clear connection between the object and the action. When you write myString.length(), you are asking that particular String object for its length, not some other string. The dot operator only works when the reference actually points to an object.
NullPointerException
A null reference is a reference variable that does not point to any object. If you try to call a method on a null reference, Java throws a NullPointerException at run time. The code compiles fine, but it crashes when that line runs.
This is one of the most common run-time errors. Any object reference that has not been assigned an object with new or set to an existing object can be null, so always check that a reference points to a real object before calling a method on it.
Instance Methods Can Return Values or Be Void
Like any method, an instance method can return a value or be void. What makes it an instance method is that it is called on an object using the dot operator.
A method like getDescription() might return a value from the object, while bark() might be void but cause an action or change the object's state. Both are instance methods because they need an object to be called on.
Code Examples
A simple class to show instance method calls:
</>Java// Example: Basic instance method calls public class Dog { private String name; private int age; public Dog(String name, int age) { this.name = name; this.age = age; } // Instance method - void return type public void bark() { System.out.println(name + " says: Woof!"); } // Instance method - returns a value public String getDescription() { return name + " is " + age + " years old"; } // Instance method that modifies object state public void haveBirthday() { age++; System.out.println(name + " is now " + age + "!"); } } // Using the Dog class public class DogPark { public static void main(String[] args) { // Create objects to call methods on Dog myDog = new Dog("Buddy", 3); Dog yourDog = new Dog("Max", 5); // Call instance methods using dot operator myDog.bark(); // Prints: "Buddy says: Woof!" yourDog.bark(); // Prints: "Max says: Woof!" // Call method that returns a value String description = myDog.getDescription(); System.out.println(description); // Prints: "Buddy is 3 years old" // Call method that modifies state myDog.haveBirthday(); // Prints: "Buddy is now 4!" } }
String methods are instance methods you will use constantly:
</>Java// Example: String instance methods public class StringMethodDemo { public static void main(String[] args) { String message = "Hello World"; // length() is an instance method of String int len = message.length(); // Returns 11 // substring() is an instance method that returns a new String String firstWord = message.substring(0, 5); // "Hello" // Original string unchanged - String objects are immutable System.out.println(message); // Still "Hello World" // Method chaining - calling instance methods one after another String result = message.substring(0, 5).substring(0, 3); // "Hel" } }
What happens with null references:
</>Java// Example: Null reference problems and solutions public class NullReferenceDemo { public static void main(String[] args) { String text = null; // No actual String object // This would compile but crash at runtime! // int length = text.length(); // NullPointerException! // Safe approach - check for null first if (text != null) { int length = text.length(); // Only runs if text isn't null System.out.println("Length: " + length); } else { System.out.println("Text is null!"); } // Another example with objects Dog noDog = null; // noDog.bark(); // Would throw NullPointerException! // Assign an object to avoid null Dog realDog = new Dog("Spot", 2); realDog.bark(); // Works fine - "Spot says: Woof!" } }
Working with ArrayList instance methods:
</>Java// Example: ArrayList instance methods import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { // Create ArrayList object ArrayList<String> names = new ArrayList<String>(); // add() is an instance method names.add("Alice"); // Returns true names.add("Bob"); // Returns true // size() is an instance method int count = names.size(); // Returns 2 // get() is an instance method String first = names.get(0); // Returns "Alice" // Multiple objects, same methods ArrayList<String> colors = new ArrayList<String>(); colors.add("Red"); colors.add("Blue"); // Each object keeps its own state System.out.println(names.size()); // 2 System.out.println(colors.size()); // 2 // Same method, different objects, different results } }
How to Use This on the AP Computer Science A Exam
Code Tracing
When you see a method call in exam code, first decide if it is a class (static) method or an instance method. If there is an object reference before the dot, it is an instance method call. This helps you read code quickly and spot errors.
For tracing problems, always check whether an object reference could be null before a method is called on it. The exam often hides this as a trap: code that looks fine but would crash with a NullPointerException.
Remember that each object runs an instance method on its own data, so the same method can return different values for different objects. Track each object's state separately as you trace.
Free Response
When you write classes, you will call instance methods on objects you create or are given, including String objects and other provided classes. Use the dot operator with the correct object reference and pass arguments that match the method.
Inside a class, instance methods can use that object's instance variables, which is how you read and update an object's state. Use that to fill in method bodies correctly rather than trying to reach into another object's private data directly.
Common Trap
- Trying to call an instance method without an object (writing
length()instead oftext.length()) will not compile. - Mixing up static and instance calls:
Math.sqrt(16)is a class method called on the class, whiletext.length()is an instance method called on an object. - Calling a method on a reference that is
nullcompiles but crashes at run time.
Common Misconceptions
- "A
NullPointerExceptionis a compile error." It is a run-time error. The code compiles, then crashes when the line with thenullreference runs. - "Instance methods change the object they are called on." Some do, but many just return a value without changing state. For example,
Stringmethods never change the original string becauseStringobjects are immutable. - "You can call any method using the class name." Only class (static) methods are called on the class name. Instance methods need an object reference and the dot operator.
- "If two objects are the same class, calling a method gives the same result." Each object has its own data, so the same instance method can return different results depending on the object's state.
- "An uninitialized object reference is ready to use." A reference that was never assigned an object is
null, and calling a method on it throws aNullPointerException.
Related AP Computer Science A Guides
Vocabulary
The following words are mentioned explicitly in the College Board Course and Exam Description for this topic.Term | Definition |
|---|---|
dot operator | The symbol (.) used in Java to access instance methods and properties of an object. |
instance methods | Methods that belong to an object and are called on specific instances of a class using the dot operator. |
null reference | A reference variable that points to no object, which will cause a NullPointerException if an instance method is called on it. |
NullPointerException | An exception that occurs when a method is called on a null reference instead of a valid object. |
Frequently Asked Questions
How do you call an instance method in Java?
Use an object reference, the dot operator, the method name, and parentheses: objectReference.methodName(). Arguments go inside the parentheses if the method requires them.
What does the dot operator do in Java?
The dot operator connects an object reference to a field or method. For instance methods, it tells Java which object should run the method.
What happens if you call a method on null?
Calling a method on a null reference compiles but throws a NullPointerException when that line runs. The reference must point to an actual object before you call an instance method.
What is the difference between an instance method and a static method?
An instance method is called on an object, such as text.length(). A static method is called on the class name, such as Math.sqrt(16).
Can an instance method be void or return a value?
Yes. Instance methods can be void, meaning they do not return a value, or they can return a value. What makes them instance methods is that they are called on an object.
How does Topic 1.14 show up on the AP CSA exam?
AP CSA questions often ask you to trace method calls, identify the object before the dot, determine returned values or side effects, and notice when a null reference would cause a NullPointerException.