Java OOP: 16 flashcards
All 16 flashcards from this kit, free and with no sign-up.
1. What does the `this` keyword refer to in Java?
Show the answer
`this` is a reference variable that refers to the current object — the object on which the method or constructor is being invoked.
2. Why is `this` needed to resolve variable shadowing in a constructor or setter?
Show the answer
When a parameter or local variable has the same name as an instance variable, `this.variableName` explicitly refers to the instance variable, while the plain name refers to the parameter/local.
3. Write a Java statement using `this` to assign a constructor parameter `name` to the instance variable `name`.
Show the answer
`this.name = name;`
4. How is `this()` used inside a constructor?
Show the answer
`this()` calls another constructor in the same class. It must be the first statement in the constructor.
5. What is constructor chaining with `this()`?
Show the answer
Using `this(...)` in one constructor to invoke another constructor of the same class, avoiding duplicate initialization code.
6. Can `this` be used inside a `static` method? Why or why not?
Show the answer
No. `static` methods belong to the class, not to any object, so there is no current object for `this` to refer to. Using `this` in a static context causes a compile-time error.
7. What does `return this;` do in a method?
Show the answer
It returns a reference to the current object, enabling method chaining (e.g., `obj.setX(1).setY(2);`).
8. How do you pass the current object as an argument to another method using `this`?
Show the answer
Use `this` as the argument: `someMethod(this);` — this passes a reference to the current object.
9. What is the difference between `this` and `this()`?
Show the answer
`this` is a reference to the current object. `this()` is a constructor call to another constructor in the same class.
10. Can `this` be used to access a static variable?
Show the answer
Yes, syntactically `this.staticVar` compiles, but it is discouraged because static members belong to the class, not the object. Use `ClassName.staticVar` instead.
11. In a method, how does `this` help when a local variable shadows an instance variable?
Show the answer
`this.instanceVar` accesses the instance variable, while the local variable is accessed by its plain name.
12. What happens if you use `this()` and it is not the first statement in a constructor?
Show the answer
A compile-time error occurs. `this()` must be the first statement in the constructor.
13. How can `this` be used to invoke an instance method of the current class?
Show the answer
`this.methodName();` — though usually optional, it can clarify that the method belongs to the current object.
14. What is the output of the following code? ```java class Test { int x = 10; void setX(int x) { this.x = x; } public static void main(String[] args) { Test t = new Test(); t.setX(20); System.out.println(t.x); } } ``…
Show the answer
`20` — `this.x = x;` assigns the parameter `x` (20) to the instance variable `x`.
15. Why is `this` useful for returning the current object in a builder pattern?
Show the answer
It allows chained method calls on the same object, making code more readable and fluent.
16. Can `this` be reassigned (e.g., `this = new Object();`)?
Show the answer
No. `this` is a final reference and cannot be reassigned.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages