0
class Box {

double width;
double height;
double depth;
double vol;



public Box(final Box box) {
    this.width = box.width;
    this.height = box.height;
    this.depth = box.depth;

    box.vol += 10_000;
}

You see I was creating an class called Box, and I have overloaded the construction. I realized that using final keyword to stop the parameter being assigned, but it cannot stop the parameter to change its' parameters.

So I was wondering if there is a way to stop this constructing function changing the parameter's attribute?

Howe Chen
  • 143
  • 2
  • 12
  • 1
    The parameters inside Box class need to be final to acheive that. Now you cant swap box parameter object. – Lemonov Feb 21 '17 at 11:34
  • You can set `vol` immutable, use a different instance to prevent any change (a clone) or ilike Lemonov said, set those final too (probably way more solution ;) ) – AxelH Feb 21 '17 at 11:35
  • 1
    What you are referring to is called an immutable object: http://stackoverflow.com/questions/279507/what-is-meant-by-immutable – OH GOD SPIDERS Feb 21 '17 at 11:36

0 Answers0