tags:

views:

92

answers:

2

I am creating an object in Java. One of its attributes is a grid reference - ints x, y & z.

Rather than creating a variable for each, is there a better way to store these?

+2  A: 

Create a record-like class, GridReference:

public class GridReference {

    public int x;

    public int y;

    public int z;

}

You could instantiate it as:

GridReference gridReference = GridReference();

And assign the individual values via:

gridReference.x = 1;
gridReference.y = 0;
gridReference.z = 0;

Accessible via:

gridReference.x;
gridReference.y;
gridReference.z;

You could flesh the class-out to a more secure object as:

public class GridReference {

    private final int _x;

    private final int _y;

    private final int _z;

    public GridReference(int x, int y, int z) {
        _x = x;
        _y = y;
        _z = z;
    }

    public getX() {
        return _x;
    }

    public getY() {
        return _y;
    }

    public getZ() {
        return _z;
    }

}

And instantiate it as:

GridReference gridReference = new GridReference(1, 0, 0);

Assigning the values at the same time. These would be accessed via:

gridReference.getX();
gridReference.getY();
gridReference.getZ();

(To change the values, you'd need to reassign the reference to a new GridReference though.)

Beau Martínez
Check your code - vars are private, but you're accessing them directly.
Robert Munteanu
my mind was a mess there.
Beau Martínez
The second option is much better. If you do myobject.gridref = a; and then myotherobject.gridref = a; a change in myobject.gridref.x = -1; will mess up in myotherobject grid reference which is not always desirable. By using the second approach this cannot happen.
OscarRyz
...as long as both gridref objects reference the same GridReference.
Beau Martínez
The immutable version is the way to go. Some of the java.awt objects cause problems every time they are used. Your code would be better with the class as final, hide the constructor (but add a public static creation method), probably add equals/hashCode/toString and definitely get rid of the underscores.
Tom Hawtin - tackline
@tom you don't like the underscores? i find it a more elegant, interface-oriented solution that, say GridReference(int xVal, int yVal, int zVal), that just reads ugly! i've seen the approach in textbooks as well.
Beau Martínez
@Beau Martínez: That's where the "this" keyword comes in handy :) instead of having to say "xVal", just say "x", then when it comes to assigning values to the the fields, say "this.x = x;"
coobird
A: 

You can use an array for them

class MyClass { 
    private final int [] gridRef = new int[3];

    public MyClass( int x, int y, int z ) { 
        gridRef[0] = x;
        gridRef[1] = y;
        gridRef[2] = z;
    }
    public int getX() { 
        return gridRef[0];
    }
    public int getY() { 
        return gridRef[1];
    }
    public int getZ() { 
        return gridRef[2];
    }
}

That would be a read only access to the grid reference.

I'm not sure if that is "better" though. What do you want to achieve?

OscarRyz
Check your code - the array elements aren't getting initialised via the constructor.
Beau Martínez
Even assuming the get methods are supposed to be public, that is some weird code.
Tom Hawtin - tackline
They are now. Well, for a weird question a weird answer is in order :) He just said: "Rather than creating a variable for each, is there a better way to store these?"
OscarRyz