Alon
Alon

Reputation: 2929

Java objacts makenew object that override current object

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

Answers (2)

Neel
Neel

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

Wyzard
Wyzard

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

Related Questions