AlexVPerl
AlexVPerl

Reputation: 8026

Can you override THIS keyword in JAVA

Dealing with a refactoring scenario, and wondering if there's a way to override "this" in java.

Basically, I would like to return an instance of another class when this is called.

Is this possible?

Upvotes: 2

Views: 974

Answers (2)

TNT
TNT

Reputation: 2920

There is no way to override this. Create an instance of the desired class and return that instead.

Test t = new Test();
public Test getTest() {
    return this.t;
}

Upvotes: 3

terary
terary

Reputation: 1108

This is a fundemental of java (and many others). As stated in the comments the thing to do is create your 'that' class and return it.

Upvotes: 1

Related Questions