Struct like objects in Java

Posted by cdv on Stack Overflow See other posts from Stack Overflow or by cdv
Published on 2008-08-31T09:17:01Z Indexed on 2010/04/15 3:03 UTC
Read the original article Hit count: 146

Filed under:
|
|
|

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();
}

© Stack Overflow or respective owner

Related posts about java

Related posts about design