Instance methods in Java - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to run instance methods changes as the input grows.
How does calling an instance method affect the program's speed when data size changes?
Analyze the time complexity of the following code snippet.
public class Counter {
private int count = 0;
public void increment() {
count++;
}
public int getCount() {
return count;
}
}
This code defines an instance method that increases a number and another that returns it.
Look for loops or repeated actions inside the methods.
- Primary operation: Simple increment or return of a value.
- How many times: Each method runs once per call, no loops inside.
Since the methods do a fixed small task, time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 simple step |
| 100 | 1 simple step |
| 1000 | 1 simple step |
Pattern observation: Time grows directly with how many times you call the method, but each call is very fast and simple.
Time Complexity: O(1)
This means each instance method call takes the same small amount of time, no matter the input size.
[X] Wrong: "Instance methods always take longer if the object has more data."
[OK] Correct: The time depends on what the method does, not just the object size. Simple methods run quickly regardless.
Understanding how instance methods work helps you explain code efficiency clearly and confidently in interviews.
"What if the instance method included a loop over a list inside the object? How would the time complexity change?"
Practice
instance methods in Java?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]
- Thinking instance methods are static
- Calling instance methods without an object
- Believing instance methods can't use 'this'
display() of an object obj?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]
- Calling instance method without object
- Using class name for instance method call
- Trying to call instance method as static
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();
}
}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]
- Expecting null because of forgetting setModel call
- Confusing instance and static variables
- Thinking printModel returns a value
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();
}
}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]
- Not using 'this' to distinguish variables
- Expecting printName to throw error
- Thinking setName returns a value
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?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]
- Using static variables for per-object data
- Missing method parameters
- Making instance methods static incorrectly
