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
Recall & Review
beginner
What is an instance method in Java?
An instance method is a method that belongs to an object of a class. It can access instance variables and requires an object to be called.
Click to reveal answer
beginner
How do you call an instance method in Java?
You call an instance method by using an object of the class followed by a dot and the method name, like <code>objectName.methodName()</code>.
Click to reveal answer
intermediate
Can an instance method access static variables directly?
Yes, an instance method can access static variables directly because static variables belong to the class, not to any specific object.
Click to reveal answer
intermediate
What is the difference between an instance method and a static method?
An instance method requires an object to be called and can access instance variables. A static method belongs to the class itself and can be called without creating an object.
Click to reveal answer
intermediate
Why can't you call an instance method from a static context without an object?
Because instance methods need an object to know which instance's data to work with. Static context has no specific object, so it cannot call instance methods directly.
Click to reveal answer
How do you call an instance method named display on an object car?
Acar.display()
Bdisplay.car()
CCar.display()
Ddisplay()
✗ Incorrect
You call an instance method using the object name followed by a dot and the method name: car.display().
Which of these is true about instance methods?
AThey cannot access static variables.
BThey can be called without creating an object.
CThey belong to the class, not objects.
DThey can access instance variables.
✗ Incorrect
Instance methods belong to objects and can access instance variables. They require an object to be called.
Can an instance method access static variables?
ANo, only static methods can access static variables.
BYes, instance methods can access static variables.
COnly if the static variable is public.
DOnly if the instance method is static.
✗ Incorrect
Instance methods can access static variables because static variables belong to the class.
What happens if you try to call an instance method from a static method without an object?
AIt works fine.
BIt calls the method on a default object.
CIt causes a compile-time error.
DIt runs but with null values.
✗ Incorrect
You get a compile-time error because instance methods need an object to be called.
Which keyword is used to refer to the current object inside an instance method?
Athis
Bself
Ccurrent
Dobj
✗ Incorrect
The keyword this refers to the current object inside an instance method.
Explain what an instance method is and how it differs from a static method.
Think about whether you need an object to call the method.
You got /4 concepts.
Describe why you cannot call an instance method directly from a static method without an object.
Consider what 'static' means about belonging to the class.
You got /3 concepts.
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
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 use this to refer to the current object.
Final Answer:
They belong to objects and can access instance variables. -> Option A
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
Step 1: Recall instance method call syntax
Instance methods are called using the object name followed by dot and method name, like obj.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 D
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
Step 1: Analyze method calls
The object c calls setModel("Tesla"), which sets the instance variable model to "Tesla".
Step 2: Check printModel output
printModel() prints the current value of model, which is "Tesla".
Final Answer:
Tesla -> Option B
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
Step 1: Check setName method assignment
The line name = name; assigns the parameter to itself, not to the instance variable.
Step 2: Understand effect on output
Because instance variable name is not set, printName() prints null.
Final Answer:
The method setName does not assign the parameter to the instance variable. -> Option C
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?