Struct like objects in Java

Is it completely against the Java way to create struct like objects?

class SomeData1 {
    public int x;
    public int y;
}

I can see a class with accessors and mutators being more Java like.

class SomeData2 {
    int getX();
    void setX(int x);

    int getY();
    void setY(int y);

    private int x;
    private int y;
}

The class from the first example is notationally convenient.

// a function in a class
public int f(SomeData1 d) {
    return (3 * d.x) / d.y;
}

This is not as convenient.

// a function in a class
public int f(SomeData2 d) {
    return (3 * d.getX()) / d.getY();
}

20 Answers
20

Leave a Comment