OOP - fields versus methods

This question is not specific to Java, however code samples are in Java.

Sometimes you come across very simple objects, such as the following code:

public class Coordinates {
    private int x;
    private int y;

    public Coordinates (int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setX (int x) { this.x = x; }
    public void setY (int y) { this.y = y; }
    public int  getX ()        { return x; }
    public int  getY ()        { return y; }
}

I would say that it is much more efficient to access fields directly, and not through methods. Also, the code would look simpler. The use itself would be simpler, and class definition would be a free (unnecessary boiler room code) 4 method than actually not doing anything. (In a sense, I am thinking of a usage similar structto that in C or recordin Pascal.)

The way I do this is probably like this:

public class Coordinates {
    public int x;
    public int y;

    public Coordinates (int x, int y) {
        this.x = x;
        this.y = y;
    }
}

, ? , - . , , ? , . , , , , , set, (, ).

, , , , . ?

+4
2

4 /. , 2 public /.

, , , /. , public .

0

:

(1) - , , .get IDE . , , .

(2) - , Jackson Framework, beans . , , , . , , .

(3) Concurrency - , . getter seters, , . , . , , . , , , , .

(4) - - .

0

All Articles