1

What I'm trying to to do is the following:

class myClass {
    myClass(myClass o) {
        //copies the variables of o into this class
    }

    void foo() {
        myClass temp = new myClass(this);
    }
}

Is this viable to making 2 instances with the exact variables inside foo()?

ernest_k
  • 44,416
  • 5
  • 53
  • 99

1 Answers1

0

Yes this is correct no issue.

But temp will be available only inside foo() as it is a local variable.

You can modify like below

myClass foo(){
  myClass temp = new myClass(this);
  return temp;
}

Now you can use

myClass obj1 = new myClass();
myClass obj2 = obj1.foo();

Now obj1 and obj2 will be two different instances having same values in the variables.

muasif80
  • 5,586
  • 4
  • 32
  • 45