Bird
Raised Fist0
Javaprogramming~10 mins

Instance methods in Java - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Instance methods
Create Object
Call Instance Method
Method Uses Object's Data
Method Executes and Returns
Back to Caller
An instance method is called on an object, uses that object's data, runs its code, then returns control back.
Execution Sample
Java
class Dog {
  String name;
  void bark() {
    System.out.println(name + " says Woof!");
  }
}

public class Main {
  public static void main(String[] args) {
    Dog d = new Dog();
    d.name = "Buddy";
    d.bark();
  }
}
This code creates a Dog object named Buddy and calls its bark method to print a message.
Execution Table
StepActionObject State (name)Output
1Create Dog object dnull
2Set d.name = "Buddy"Buddy
3Call d.bark()BuddyBuddy says Woof!
4Method bark() finishesBuddy
💡 Method bark() completes and program ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
d.namenullnullBuddyBuddyBuddy
Key Moments - 2 Insights
Why does the method bark() use the object's name?
Because bark() is an instance method, it uses the current object's data (d.name) as shown in step 3 of the execution_table.
What happens if we call bark() before setting name?
The output would show 'null says Woof!' because d.name is null before step 2, as seen in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of d.name after step 2?
A"Buddy"
Bnull
C"Woof!"
Dundefined
💡 Hint
Check the 'Object State (name)' column at step 2 in execution_table
At which step does the program print output?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' column in execution_table to find when output appears
If we create another Dog object without setting name, what will bark() print?
A"Buddy says Woof!"
B"null says Woof!"
C" says Woof!"
DNo output
💡 Hint
Refer to key_moments about calling bark() before setting name and variable_tracker initial state
Concept Snapshot
Instance methods belong to objects.
They use the object's data (fields).
Called with object.methodName().
Can access and modify object state.
Return control after execution.
Full Transcript
Instance methods in Java are functions that belong to objects. When you create an object from a class, you can call its instance methods. These methods can use and change the object's data. For example, if you have a Dog object with a name, calling its bark method will print a message using that name. The method runs using the current object's data and then returns control back to where it was called. If you call the method before setting the object's data, it will use default or null values. This step-by-step trace shows how the object is created, its data set, the method called, and the output produced.

Practice

(1/5)
1. What is true about instance methods in Java?
easy
A. They belong to objects and can access instance variables.
B. They can be called without creating an object.
C. They must be declared as static.
D. They cannot use the this keyword.

Solution

  1. Step 1: Understand instance methods

    Instance methods belong to objects and can access the object's data (instance variables).
  2. Step 2: Check other options

    Instance methods require an object to be called, are not static, and can use this to refer to the current object.
  3. Final Answer:

    They belong to objects and can access instance variables. -> Option A
  4. Quick 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
A. display();
B. static display();
C. ClassName.display();
D. obj.display();

Solution

  1. Step 1: Recall instance method call syntax

    Instance methods are called using the object name followed by dot and method name, like obj.display();.
  2. Step 2: Check other options

    Calling without object or using class name is for static methods; static display(); is invalid syntax.
  3. Final Answer:

    obj.display(); -> Option D
  4. Quick 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
A. Compilation error
B. Tesla
C. null
D. Runtime error

Solution

  1. Step 1: Analyze method calls

    The object c calls setModel("Tesla"), which sets the instance variable model to "Tesla".
  2. Step 2: Check printModel output

    printModel() prints the current value of model, which is "Tesla".
  3. Final Answer:

    Tesla -> Option B
  4. Quick 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
A. The class Person should be declared public.
B. The printName method is missing a return statement.
C. The method setName does not assign the parameter to the instance variable.
D. The main method should be static void.

Solution

  1. Step 1: Check setName method assignment

    The line name = name; assigns the parameter to itself, not to the instance variable.
  2. Step 2: Understand effect on output

    Because instance variable name is not set, printName() prints null.
  3. Final Answer:

    The method setName does not assign the parameter to the instance variable. -> Option C
  4. Quick 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
A. class BankAccount { private double balance; void deposit(double amount) { balance += amount; } double getBalance() { return balance; } }
B. class BankAccount { static double balance; void deposit(double amount) { balance += amount; } double getBalance() { return balance; } }
C. class BankAccount { private double balance; static void deposit(double amount) { balance += amount; } static double getBalance() { return balance; } }
D. class BankAccount { double balance; void deposit() { balance += amount; } double getBalance() { return balance; } }

Solution

  1. 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 variable balance and instance methods that correctly update and return it.
  2. 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 parameter amount.
  3. Final Answer:

    class BankAccount { private double balance; void deposit(double amount) { balance += amount; } double getBalance() { return balance; } } -> Option A
  4. Quick 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