Instance methods let objects do actions using their own data. They help organize code by connecting behavior to specific things.
Instance methods in Java
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Java
class ClassName { returnType methodName(parameters) { // method body } }
Instance methods belong to objects created from the class.
You call them using the object name, like object.methodName().
Examples
bark makes the dog object say "Woof!".Java
class Dog { void bark() { System.out.println("Woof!"); } }
increment adds 1 to the object's count number.Java
class Counter { int count = 0; void increment() { count = count + 1; } }
Sample Program
This program creates a Car object with a color and then calls the instance method showColor to print the car's color.
Java
class Car { String color; Car(String color) { this.color = color; } void showColor() { System.out.println("The car color is " + color); } } public class Main { public static void main(String[] args) { Car myCar = new Car("red"); myCar.showColor(); } }
Important Notes
Instance methods can use and change the object's own data fields.
You need to create an object before calling an instance method.
Use this keyword inside instance methods to refer to the current object.
Summary
Instance methods belong to objects and use their data.
Call instance methods using the object name.
They help keep code organized and connected to the things it works with.
Practice
1. What is true about
instance methods in Java?easy
Solution
Step 1: Understand instance methods
Instance methods belong to objects and can access the object's data (instance variables).Step 2: Check other options
Instance methods require an object to be called, are not static, and can usethisto refer to the current object.Final Answer:
They belong to objects and can access instance variables. -> Option AQuick Check:
Instance methods = object-specific behavior [OK]
Hint: Instance methods need an object to work with [OK]
Common Mistakes:
- Thinking instance methods are static
- Calling instance methods without an object
- Believing instance methods can't use 'this'
2. Which of the following is the correct way to call an instance method
display() of an object obj?easy
Solution
Step 1: Recall instance method call syntax
Instance methods are called using the object name followed by dot and method name, likeobj.display();.Step 2: Check other options
Calling without object or using class name is for static methods;static display();is invalid syntax.Final Answer:
obj.display(); -> Option DQuick Check:
Call instance method with object name [OK]
Hint: Use objectName.methodName() to call instance methods [OK]
Common Mistakes:
- Calling instance method without object
- Using class name for instance method call
- Trying to call instance method as static
3. What will be the output of this code?
class Car {
String model;
void setModel(String m) {
model = m;
}
void printModel() {
System.out.println(model);
}
}
public class Test {
public static void main(String[] args) {
Car c = new Car();
c.setModel("Tesla");
c.printModel();
}
}medium
Solution
Step 1: Analyze method calls
The objectccallssetModel("Tesla"), which sets the instance variablemodelto "Tesla".Step 2: Check printModel output
printModel()prints the current value ofmodel, which is "Tesla".Final Answer:
Tesla -> Option BQuick Check:
Instance variable set then printed = Tesla [OK]
Hint: Instance methods change and show object data [OK]
Common Mistakes:
- Expecting null because of forgetting setModel call
- Confusing instance and static variables
- Thinking printModel returns a value
4. Identify the error in this code snippet:
class Person {
String name;
void setName(String name) {
name = name;
}
void printName() {
System.out.println(name);
}
}
public class Main {
public static void main(String[] args) {
Person p = new Person();
p.setName("Alice");
p.printName();
}
}medium
Solution
Step 1: Check setName method assignment
The linename = name;assigns the parameter to itself, not to the instance variable.Step 2: Understand effect on output
Because instance variablenameis not set,printName()prints null.Final Answer:
The method setName does not assign the parameter to the instance variable. -> Option CQuick Check:
Parameter shadows instance variable, no assignment [OK]
Hint: Use this.name = name to assign instance variable [OK]
Common Mistakes:
- Not using 'this' to distinguish variables
- Expecting printName to throw error
- Thinking setName returns a value
5. You want to create a class
BankAccount with an instance method deposit that adds money to the account balance, and another method getBalance that returns the current balance. Which code correctly implements these instance methods?hard
Solution
Step 1: Check instance variable and method signatures
class BankAccount { private double balance; void deposit(double amount) { balance += amount; } double getBalance() { return balance; } } uses a private instance variablebalanceand instance methods that correctly update and return it.Step 2: Verify other options
class BankAccount { static double balance; void deposit(double amount) { balance += amount; } double getBalance() { return balance; } } uses static variable, which shares balance across all accounts. class BankAccount { private double balance; static void deposit(double amount) { balance += amount; } static double getBalance() { return balance; } } uses static methods incorrectly. class BankAccount { double balance; void deposit() { balance += amount; } double getBalance() { return balance; } }'s deposit method lacks parameteramount.Final Answer:
class BankAccount { private double balance; void deposit(double amount) { balance += amount; } double getBalance() { return balance; } } -> Option AQuick Check:
Instance methods update and return object-specific data [OK]
Hint: Instance methods use instance variables, not static, with parameters [OK]
Common Mistakes:
- Using static variables for per-object data
- Missing method parameters
- Making instance methods static incorrectly
