PrepGo

Calling Instance Methods - AP Computer Science A Study Guide

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Learn with study guides reviewed by top AP teachers. This guide takes about 9 minutes to read.

Getting Started

In object-oriented programming, a class acts as a blueprint for creating objects. Once we have created these objects, or "instances," we need a way to interact with them and make them perform actions. This chapter focuses on the fundamental mechanism for commanding objects to execute their behaviors: calling instance methods.

What You Should Be Able to Do

  • Use the dot operator (.) to call a method on a specific object.

  • Pass required arguments to an instance method during a call.

  • Explain how calling an instance method affects the state (the instance variables) of that specific object.

  • Trace the state of multiple objects of the same class as methods are called on them.

  • Distinguish between an object reference variable and the method being called.

Key Concepts & Java Implementation

The Core Idea

An object is a specific instance of a class, containing its own set of data (state) and behaviors (methods). Think of a Dog class as the blueprint for all dogs. An object, like fido, is a real dog created from that blueprint. fido has his own state, such as his age and name.

An instance method is a behavior that belongs to a particular object. To make fido bark, you don't just yell "bark" into the void; you command fido specifically. In Java, we do this by "sending a message" to the object. This message is the method call. When we call the bark() method on the fido object, it's fido who barks. If we have another Dog object named spot, calling spot.bark() makes spot bark, and this action is completely independent of fido. The method call is always tied to the object it is called on, and it operates on that object's unique state.

Syntax & Implementation

The primary tool for calling an instance method is the dot operator (.). It connects the object reference variable to the method you wish to invoke.

  • Syntax Table: Calling an Instance Method
ComponentPurposeJava Example
objectReferenceThe variable that holds a reference to the object.myRobot
. (Dot Operator)Connects the object to one of its methods..
methodName()The name of the method to be executed.moveForward()
(arguments)Values passed to the method, if required.(10)
  • Annotated Java Examples

Let's consider a simple Robot class. Each Robot object has its own position and power status.


// Robot.java - The class blueprint

public class Robot {

    // An instance variable stores the state for each specific object.

    private int position; 

    private boolean isPoweredOn;


    // Constructor to initialize a new Robot object's state.

    public Robot() {

        this.position = 0;

        this.isPoweredOn = false;

    }


    // An instance method to change the object's state.

    public void powerOn() {

        this.isPoweredOn = true;

        System.out.println("Robot powering on.");

    }


    // An instance method that uses the object's state.

    public void moveForward(int steps) {

        if (isPoweredOn) {

            this.position = this.position + steps;

            System.out.println("Moved forward " + steps + " steps.");

        } else {

            System.out.println("Cannot move. Robot is off.");

        }

    }


    public int getPosition() {

        return this.position;

    }

}

Now, let's create and interact with Robot objects in a separate class.


// RobotController.java - The class where we use Robot objects

public class RobotController {

    public static void main(String[] args) {

        // Create two separate, independent Robot objects.

        Robot robotA = new Robot();

        Robot robotB = new Robot();


        // Call an instance method on the robotA object.

        // This only affects robotA's state.

        robotA.powerOn(); 


        // Call another instance method on robotA.

        robotA.moveForward(5);


        // Call a method on robotB. This does not affect robotA.

        robotB.powerOn();

        robotB.moveForward(20);


        // Print the final state of each object.

        // Note that each object has its own value for the 'position' instance variable.

        System.out.println("Robot A final position: " + robotA.getPosition());

        System.out.println("Robot B final position: " + robotB.getPosition());

    }

}

Tracing & Analysis

  • Execution Trace

Let's trace the state of the position instance variable for robotA and robotB from the RobotController example.

Line of CoderobotA.positionrobotB.positionOutput
Robot robotA = new Robot();0(not created)
Robot robotB = new Robot();00
robotA.powerOn();00Robot powering on.
robotA.moveForward(5);50Moved forward 5 steps.
robotB.powerOn();50Robot powering on.
robotB.moveForward(20);520Moved forward 20 steps.
System.out.println(...)520Robot A final position: 5
System.out.println(...)520Robot B final position: 20
  • Analysis

The trace clearly shows that the method call robotA.moveForward(5) only modified the position variable belonging to the robotA object. The state of robotB remained unchanged until a method was explicitly called on it. This separation of state is a core principle of object-oriented programming, allowing us to model multiple independent entities using a single class blueprint.

Java Syntax Quick-Reference

  • objectReference.methodName(arguments): The standard syntax for invoking an instance method on an object. The method will operate on the state of the object referred to by objectReference.

  • . (Dot Operator): The operator that links an object reference to one of its members (in this case, a method).

Core Code Examples & Terminology

  • Object: An instance of a class, created using the new keyword. An object has its own state (data stored in instance variables) and a set of behaviors (instance methods).

  • Instance Method: A non-static method defined in a class. It belongs to a specific object and can directly access and modify that object's instance variables.

  • Instance Variable: A variable declared inside a class but outside any method. Each object of the class gets its own separate copy of the instance variables.

  • Dot Operator (.): The symbol used in Java to access an object's members. It is placed between the object reference and the member name (e.g., myDog.getAge()).

  • Core Snippet 1: Creating an Object

    
    // Creates a new Student object and assigns its reference to the 's1' variable.
    
    Student s1 = new Student();
    

    This line of code instantiates a new object from the Student class.

  • Core Snippet 2: Calling a Method Without Arguments

    
    // Calls the 'incrementGradeLevel' method on the object referenced by 's1'.
    
    s1.incrementGradeLevel();
    

    This invokes a behavior on the s1 object, which might change its internal state.

  • Core Snippet 3: Calling a Method With an Argument

    
    // Calls the 'setGpa' method on 's1', passing the value 3.8 as an argument.
    
    s1.setGpa(3.8);
    

    This invokes a behavior and provides it with data needed to perform its task.

Core Skill Check

  • Code Tracing: What is the final value of c2.getValue() after this Java code runs?

    
    // Assume a Counter class with a 'value' instance variable and an 'increment()' method.
    
    Counter c1 = new Counter(); // c1.value is 0
    
    Counter c2 = new Counter(); // c2.value is 0
    
    c1.increment();
    
    c2.increment();
    
    c1.increment();
    
    // Answer: 1
    
  • Debugging: Identify the compile-time error in this Java code, assuming turnLeft() is an instance method of the Car class.

    
    Car myCar = new Car();
    
    Car.turnLeft(); // Error is here
    
    // Error: turnLeft() is an instance method and cannot be called on the class name 'Car'. It must be called on an object, like 'myCar.turnLeft()'.
    
  • Application: Given a Rectangle object named box with a method setWidth(double newWidth), write a single line of Java to set its width to 50.5.

    
    box.setWidth(50.5);
    

Common Misconceptions & Errors

  • Calling an Instance Method on a Class: You cannot call a non-static (instance) method using the class name (e.g., Robot.moveForward(5)). Instance methods must be called on a specific object (e.g., robotA.moveForward(5)).

  • Forgetting the Object Reference: In a main method or other static context, you cannot call an instance method directly (e.g., moveForward(5)). You must specify which object should perform the action (e.g., myRobot.moveForward(5)).

  • NullPointerException: If an object variable has not been initialized (it is null), attempting to call a method on it will cause a NullPointerException at runtime. For example: Robot r3 = null; r3.powerOn(); will crash the program.

Summary

Calling instance methods is the primary way we interact with objects and make them perform work. We use the dot operator (.) to connect a specific object reference to the method we want to execute. Each method call is bound to the object it is called on, meaning it accesses and modifies the unique state (instance variables) of that object. This allows us to create and manage many independent objects from a single class blueprint, which is the foundation of building complex, real-world applications in Java.