Java OOP: quick revision, 12 bites
All 12 revision bites from this kit, free and with no sign-up.
`this` is a reference to the current object
Your object's self-portrait.
Inside any **instance method** or **constructor**, `this` is a reference variable that points to the object on which the method was invoked. It lets an object refer to itself.
Takeaway. `this` always means 'the object I am working on right now'.
What exactly is `this`?
Not a variable you declare.
`this` is a **final reference** that Java automatically provides inside every instance context. You never declare it; it is implicitly available in instance methods, constructors, and instance initializer blocks.
Takeaway. `this` is an implicit, read-only reference to the current object.
Using `this` inside a static method
Static has no object.
A **static method** belongs to the class, not to any object. Since there is no current object, `this` cannot be used there. The compiler will reject it.
Takeaway. No object, no `this` — static methods cannot use it.
Shadowing: field vs parameter
Same name, different scope.
When a **constructor parameter** or **local variable** has the same name as an **instance field**, the local one shadows the field. Use `this.fieldName` to refer to the field explicitly.
Takeaway. `this.` disambiguates a shadowed field from a local variable.
Constructor chaining with `this()`
One constructor calls another.
Inside a constructor, `this(...)` invokes another constructor of the **same class**. This reduces code duplication. It must be the **first statement** in the constructor.
Takeaway. `this(...)` chains constructors within the same class.
`this()` vs `super()`
Same class or parent?
Takeaway. `this()` stays in the class; `super()` goes up to the parent.
Returning the current object
Fluent interfaces start here.
A method can return `this` to allow **method chaining**. This is common in builder patterns and fluent APIs.
Takeaway. `return this;` enables chained method calls.
Passing `this` as an argument
Hand your object over.
You can pass `this` to another method or constructor when the current object needs to be used elsewhere, such as registering a listener or passing state to a helper.
Takeaway. `this` can be passed like any other reference.
`this` is not available in static context
Static belongs to the class.
**Static methods**, **static blocks**, and **static nested classes** have no `this` because they are not tied to an instance. Trying to use `this` there causes a compile-time error.
Takeaway. Static context = no current object = no `this`.
Remember: `this` = 'me, the object'
Point to yourself.
Whenever you see `this`, read it as **'this object'** or **'me'**. If you are in a static method, there is no 'me' — only the class.
Takeaway. `this` is the object's way of saying 'me'.
Exam trap: `this` in constructors
First line only.
If you use `this(...)` to call another constructor, it **must be the first statement**. Placing it later causes a compile-time error. Also, you cannot use `this()` and `super()` together in the same constructor.
Takeaway. `this()` must be the first statement in a constructor.
`this` is a keyword, not a variable
Reserved and special.
`this` is a **reserved keyword** in Java. You cannot use it as an identifier, and you cannot assign to it. It is always provided by the JVM.
Takeaway. `this` is a keyword — treat it as read-only.
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