UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.14
1.14 Calling Instance Methods
Instance methods operate on a specific object. The call syntax is object-dot-method, and the exam expects you to classify methods by what they do.
What you need to know
- Instance methods are called on an object:
obj.method(args). The method operates on that object's instance variables. - An accessor (getter) returns information about the object's state without changing it:
getName(),length(). - A mutator (setter) changes the object's state; typically
void:setAge(5),deposit(100). - Method calls can be chained if each returns an object:
s.substring(1).toUpperCase(). - The dot operator on a null reference throws NullPointerException. The dot operator on a primitive doesn't compile (
inthas no methods). - Calling a method with the wrong number or types of arguments, or calling a method the class doesn't have, is a compile-time error.
Worked example
Account acct = new Account("Ram", 100.0);
acct.deposit(50.0); // mutator: changes balance
double b = acct.getBalance(); // accessor: 150.0
String who = acct.getOwner(); // accessor: "Ram"
acct.getBalance(); // legal but pointless: value discarded
Exam tip: When a question gives you a class's method headers and asks what a code segment does, check each call: is it a mutator (state changed for later lines) or an accessor (value used now)? Track object state in a table just like variables.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Instance methods are called on an object and have access to that object's instance variables.
acct.deposit(50)changes that account's balance. Calling it on a different account changes a different balance. - Accessor (getter): returns state, doesn't change it. Convention:
getX(), orisX()for booleans. Mutator (setter): changes state, usually void. Convention:setX(value), or verbs likedeposit,increment. - A method that both changes state and returns something exists (ArrayList's
removedoes), but the exam's own classes mostly follow the accessor/mutator split cleanly. - Method chaining works when a method returns an object:
s.substring(2).toUpperCase().length(). Read left to right: each call operates on what the previous one returned. - The dot operator requires a reference to an object. On a primitive (
int x; x.foo()) it's a compile error. On null it's a run-time error. On a valid reference to a class without that method, compile error. - Calling an accessor and ignoring the result is legal and useless:
acct.getBalance();on its own line does nothing observable.
Mistakes that cost points
- Calling a mutator and expecting a return value.
double b = acct.deposit(50);where deposit is void — compile error. - Calling a method on a primitive.
int n = 5; n.toString()— compile error. Primitives have no methods. - Not tracking state across calls. Two mutator calls in a row compound. Update the object's state in your table after each.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 A class
Temperature has a method public double getDegrees() and a method public void setDegrees(double d). Which of the following is true?Show answer
Answer: B. get returns state (accessor); set changes it (mutator).
Q2 Which of the following will not compile?
Show answer
Answer: B. Primitives have no methods. Calling .length() on an int is a compile-time error.
Key vocabulary
- Instance method
- a method called on an object that operates on that object's data
- Accessor
- a method that returns information about an object without modifying it
- Mutator
- a method that changes an object's state