1. Using Objects and Methods

Calling Instance Methods

Calling Instance Methods

students, imagine you are using a smartphone app πŸ“±. You do not need to know how every feature is built inside the app to use it. You tap a button, and the app performs an action. In Java, calling an instance method works in a similar way: you use an object, ask it to do something, and it responds with a result or change. This lesson explains how instance methods work, how to call them correctly, and why they matter in AP Computer Science A.

By the end of this lesson, you should be able to:

  • explain what an instance method is,
  • identify the object, method name, and arguments in a method call,
  • predict what a method call returns or changes,
  • use instance methods correctly in Java code,
  • connect instance methods to objects, classes, and APIs.

What Is an Instance Method?

An instance method is a method that belongs to a specific object, not just to the class in general. If a class is like a blueprint for building houses, then an object is one house built from that blueprint. An instance method is one of the actions that house can do, such as opening a door or turning on a light.

In Java, instance methods are called using dot notation. The general pattern is:

$$\text{objectName.methodName(arguments)}$$

For example, if $s$ is a String object, you might call:

$$s.length()$$

Here, $s$ is the object, $length$ is the method name, and there are no arguments inside the parentheses.

Instance methods are important because they let you work with data stored in objects. Many Java classes in the standard library have useful instance methods. Examples include String, ArrayList, and many classes used in AP Computer Science A.

Understanding the Parts of a Method Call

A method call has several parts, and students, each part matters.

  1. Object: the specific instance that will perform the action.
  2. Dot operator: the period between the object and the method name.
  3. Method name: the action being requested.
  4. Arguments: the values passed into the method, if any.
  5. Return value: the result produced by the method, if it has one.

Example:

$$\text{word.substring}(1, 4)$$

In this call, $word$ is the object, $substring$ is the method name, and $1$ and $4$ are arguments. The method returns a new String made from characters starting at index $1$ and ending before index $4$.

A common mistake is confusing the object with the method. Remember: the object comes before the dot, and the method name comes after it.

Another important idea is that some methods return a value, while others do not. For example, $\text{length}()$ returns an $int$, but a method like $\text{println}()$ in $System.out$ performs an action and does not return a value that you usually store.

Calling Instance Methods on Strings

Strings are one of the easiest places to practice calling instance methods. A String object stores a sequence of characters, and many methods help you inspect or create new Strings.

Suppose:

$$\text{String message = "Hello";}$$

You can call:

$$\text{message.length()}$$

This returns $5$ because the String has five characters.

You can also call:

$$\text{message.charAt}(1)$$

This returns the character $e$, because Java uses zero-based indexing. That means the first character is at index $0$, the second at index $1$, and so on.

Another useful method is:

$$\text{message.substring}(1, 4)$$

This returns $\text{"ell"}$ because it starts at index $1$ and goes up to, but does not include, index $4$.

Strings are especially helpful for learning instance methods because they do not change when you call most methods on them. For example, if you call $\text{message.substring}(1)$, the original String stays the same, and the method returns a new String.

That idea is called immutability. In AP Computer Science A, it is important to know that many String methods return a new String instead of changing the original one.

Returning Values and Using Them in Code

students, one of the most important skills is using the return value of an instance method. A method call can be used as part of a larger expression.

For example:

$$\text{int len = message.length();}$$

Here, the result of $\text{message.length()}$ is stored in $len$.

You can also use method calls inside conditions:

$$\text{if } (\text{message.length()} > 3)$$

This condition is true when the String has more than three characters.

Method calls can also be nested. For example:

$$\text{message.substring}(0, \text{message.length}())$$

This means β€œtake the substring from index $0$ to the full length of the String.” Since the end index is exclusive, this returns the entire String.

When using return values, make sure the type matches what the code expects. If a method returns a $String$, you cannot store it in an $int$ variable. Java is strict about types, and that is a big part of why method calls must be used carefully.

Methods That Change Objects

Not all instance methods return new values without changing anything. Some methods modify the state of the object they belong to. This is common with mutable classes like ArrayList.

For example, if you have:

$$\text{ArrayList<String> names = new ArrayList<String>();}$$

Then you can call:

$$\text{names.add("Ava")}$$

This method changes the ArrayList by adding a new element. After the call, the list contains $\text{"Ava"}$.

You might also call:

$$\text{names.remove(0)}$$

This removes the element at index $0$.

For methods like these, it is essential to understand the difference between the return value and the side effect. A side effect is a change made to the object itself. In AP Computer Science A, a method may both change the object and return a value, or it may do one of those things only.

A real-world comparison is a notebook πŸ“. Writing a new note changes the notebook. That is like calling a method that modifies an object.

Why the Object Matters

Instance methods depend on the object you call them on. The same method name can give different results depending on the object.

For example:

$$\text{"cat".length()}$$

returns $3$, while:

$$\text{"computer".length()}$$

returns $8$.

The method is the same, but the objects are different, so the result changes.

This is a key idea in object-oriented programming: objects store their own data and methods act on that data. When you call an instance method, you are asking that specific object to do something with its own state.

This also helps explain why you must create an object before calling many instance methods. If you want to use a method on a String, you need a String object. If you want to use a method on an ArrayList, you need an ArrayList object.

Common AP CSA Mistakes to Avoid

Several mistakes show up often on AP Computer Science A questions:

  • forgetting the object before the dot,
  • using the wrong number of arguments,
  • mixing up parentheses and square brackets,
  • misunderstanding zero-based indexing,
  • assuming a String method changes the original String,
  • storing a returned value in the wrong variable type.

Example of a correct call:

$$\text{word.indexOf("a")}$$

Example of an incorrect call:

$$\text{indexOf(word, "a")}$$

The second example looks more like a static method call or a function call, but instance methods use the object first.

Another common issue is forgetting that many methods have an exact parameter list. For example, $\text{substring}(2, 5)$ is different from $\text{substring}(2)$. They may both be valid, but they do different things.

To solve method-call questions, students, read the code carefully, identify the object, check the method signature, and track what the method returns or changes.

How This Fits Into Using Objects and Methods

Calling instance methods is a central skill in the topic of Using Objects and Methods. It connects the class definition, object creation, method behavior, and program logic.

Here is the big picture:

  • A class defines what an object can do.
  • An object is a specific instance of that class.
  • An instance method is an action tied to that object.
  • A method call tells the object to perform that action.

This topic also connects to APIs and libraries because many useful Java classes already provide instance methods you can use. You do not have to build everything from scratch. Instead, you learn how to use what Java provides correctly.

For example, if you know how to call instance methods on String, you can solve problems involving text processing, such as checking usernames, formatting messages, or finding parts of a sentence.

Conclusion

Calling instance methods is one of the most important skills in Java programming and AP Computer Science A. students, when you understand the object, the method name, the arguments, and the return value, you can read and write code more accurately. Instance methods let objects act on their own data, which makes programs more organized and easier to understand.

As you practice, always ask: What object am I using? What method am I calling? Does it return a value, change the object, or both? If you can answer those questions, you are well on your way to mastering Using Objects and Methods πŸš€

Study Notes

  • An instance method belongs to a specific object.
  • Instance methods are called with dot notation: $\text{object.method(arguments)}$.
  • The object comes before the dot, and the method name comes after it.
  • Some instance methods return a value, and some change the object.
  • Strings are immutable, so most String methods return a new String instead of changing the original.
  • Java uses zero-based indexing for Strings and many other structures.
  • The return type must match how the method result is used in code.
  • ArrayList methods like $\text{add}()$ and $\text{remove}()$ can change the list.
  • Method calls can be used in assignments, expressions, and conditions.
  • Reading the method signature carefully helps avoid AP CSA mistakes.

Practice Quiz

5 questions to test your understanding

Calling Instance Methods β€” AP Computer Science A | A-Warded