Copy constructor using personal attributes

My first question here is so gentle.

I need arguments for the following code:

public class Example {
    private String name;
    private int age;

    ...

    // copy constructor here
    public Example(Example e) {
        this.name = e.name; // accessing a private attribute of an instance
        this.age = e.age;
    }

    ...
}

I believe this violates the modularity of the instance passed to the copy constructor. This is what I consider correct:

public class Example {
    private String name;
    private int age;

    ...
    // copy constructor here
    public Example(Example e) {
        this.setName(e.getName());
        this.setAge(e.getAge());
    }

    ...
}

A friend put forth a valid point of view, saying that in the design of the copy we must create the object as quickly as possible. And adding getter / setter methods will lead to unnecessary overhead.

I am standing at the crossroads. Can you shed some light?

+3
source share
2 answers

Access is based on classes, not based on objects.

, ther- API, . " " - .

+1

- , .

, / , "", - ( ), (, ) .

, , , . "".

, - /accessors, , / - , , /accessor.

+1

Source: https://habr.com/ru/post/1736535/


All Articles