Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java have a const reference equivalent?

Here's a snippet of code:

    //Game board is made up of Squares.  A player can place GamePieces on a Square.
    public class CheckersBoard
    {
        public boolean PlaceGamePiece(GamePiece gamePiece, int nRow, int nColumn) {
            return m_theGameBoard[nRow][nColumn].PlaceGamePiece(gamePiece);
        }

        private Square[][] m_theGameBoard;
    }

Let say I'm testing the PlaceGamePiece method (using junit) and I need to access the m_theGameBoard so I can look at it and verify the GamePiece was placed on the correct Square and has the correct data.

In C++ I'd either make the test class a friend so it can access the private member m_theGameBoard, or I'd have a function that returns a const GameBoard that cannot be modified (because it's const):

const GameBoard& GetGameBoard() const { return m_theGameBoard; }

Now I can do what ever checking I want to do on the game board, but I can't modify the game board because it's const.

Java doesn't support returning const references or friend classes. So my question is what is the standard Java way of doing this?? Do I have to just provide a bunch of get accessors that allow me check the data on the Square?

UPDATE: I ended up writing a GetPiece method as Kaleb Brasee suggested.

public GamePiece GetGamePiece(Point pt) {
    return new GamePiece(m_theGameBoard[pt.GetRow()][pt.GetColumn()]);
}

Notice I create a new GamePiece object and return that. I'm not returning the GameBoards internal reference, therefore no one can modify the gameboard because they only have a copy! Nice! Thanks for the help guys, some really good advice.

FYI: I keep changing the names of the objects when I post them on here, sorry if that confused anyone.

like image 685
cchampion Avatar asked Jan 28 '10 04:01

cchampion


People also ask

What is the Java equivalent of const?

The Java equivalent of const depends on the type of variable— primitive or object— and on what you want to make constant— the object contents or the "pointer" (reference in Java). The situation can be roughly summed up as follows: Use an immutable object or create a subclass that forces immutability.

What is the C++ equivalent of an array reference in Java?

The nearest equivalent in Java is to use a single-element primitive array. So the equivalent to this in C++: Making the array reference final has essentially the same semantics as a const primitive pointer: Array with unmodifiable contents?

What is the Java equivalent of a constant pointer to a primitive?

There's no real equivalent to the constant pointer to a primitive, since Java doesn't allow pointers to primitives in the first place. (Strictly speaking, object references aren't quite like pointers either, but we consider them functionally similar here.) The nearest equivalent in Java is to use a single-element primitive array.

How to compare two objects with equals ()?

Use == on objects to perform identity comparison. That is what the default implementation of equals () does, but one normally overrides equals () to serve as an "equivalent content" check. That's what the == operator does. The default bahaviour of equals () is to compare the two objects using the == operator.


2 Answers

Define the variable as protected, so that if the unit test is in the same package, it can access it.

However, I would just add a public method getPiece(int row, int col) that returns the piece on that square (or null if there's no piece there). It's likely you'll need a method like that anyway, and you could use it in your tests.

like image 140
Kaleb Brasee Avatar answered Oct 13 '22 14:10

Kaleb Brasee


The default access modifier for a method or property is "package-protected", which means "visible only within my package". Therefore, you can emulate the semantics of "friendship", while maintaining a separate code base for tests, by having two distinct source folders, but with the same package structure.

src
|--com
    |
    --cchampion
         |
         --MyClass.java
tests
|--com
    |
    --cchampion
         |
         --TestMyClass.java

Then, within MyClass.java

public class MyClass {
    private int nonTestedThing;
    int[][] testedThing;  // this is visible to TestMyClass
    // or
    // protected int[][] testedThing;
    // if you want it visible to kids
}

This is not ideal in any respect, but it's one way to go about it.

like image 44
Jonathan Feinberg Avatar answered Oct 13 '22 13:10

Jonathan Feinberg