Reputation: 2929
I have a class and I want something like this:
public class MainClass{
.
.
.
public makenew(){
this=new ClassName()
}
}
so when I call makenew the object changes.
Can I do this?
Upvotes: 0
Views: 35
Reputation: 2100
No, you cannot assign anything to "this"
. "this"
is a reference to the current object, but is not an L-value. This is not allowed in Java.
Upvotes: 0
Reputation: 34563
No, you can't change an existing object to be an instance of a different class. You can stop using the old object and start using a different object instead, but you can't change the identity of an object after it's created.
Upvotes: 1