0

I have extended a class in hope to store a global array (make the array within the class be seen by another object) by using the set1sub(sub.local) method

public class BattleShipsClient extends ShipLocations implements Runnable, BattleShipConstants
{

BattleShipsClient()
{ 
}

public void placeBoard(int player) throws IOException// this is only called once and not updated when clicked again 
{
    while(getLocations())

    {
        while(isWaiting())//true
        {
            toServer.writeInt(player);
    int row = getRowSelected();
    int col = getColumnSelected();
    int choice=Integer.parseInt(JOptionPane.showInputDialog("Please 1 for a sub, 2 for a battleship and 3 for a destroyer"));
    clickCount ++;

    if(clickCount >2)
    {
        setLocation(false);
        continueToPlay=true;
    }

    if (choice ==1 &&sub.placed==false)
    {
        String orientation =JOptionPane.showInputDialog("please enter your orientations");


        if(orientation.equalsIgnoreCase("h"))
        {


            //row os the same
            //column number will be sent, then plus one on the other side
            sub.local = new int[2][2];
            sub.local[0][0] =row;
            sub.local[0][1]=col;
            sub.local[1][0]=row;
            sub.local[1][1] =col+1;

            toServer.writeInt(row);
            toServer.writeInt(col);


            toServer.writeChar('s');


            sub.placed=true;
            setp1sub(sub.local);
                          /*setp1sub(new int[][]{{row,col},
                    {row,col+1}});*/


            grid[row][col+1].setToken(getMyToken());

        }

I then have a ship Locations class however when i create a new object of the ship locations class and try to read this array it always is set to [[0, 0], [0, 0]], ive tried making it static and atomic

public class ShipLocations {


int [][] p1sub;

public ShipLocations()
{
    p1sub = new int[2][2];
}
public int[][] getp1sub()
{
    return p1sub;
}
public void setp1sub(int[][] local) {
    for (int i = 0;i <local.length;i++)
    {
        for(int j = 0;j<local.length;j++)
        {
            p1sub [i][j]= local[i][j];
        }
    }

}



}
5
  • 2
    How are you verifying the array is not changed? have you tried debugging? Seems to me the ShipLocations code is correct. Commented Jan 15, 2013 at 9:29
  • @Ivaylo in a GameSession class i create a new shiplocations object then call it by if(sl.p1sub==null){System.out.println("array is null");} System.out.println(Arrays.deepToString(sl.getp1sub())); Commented Jan 15, 2013 at 9:51
  • 1
    what you say here significantly differs from your question. Is your array always {{0,0},{0,0} or is it always null? Commented Jan 15, 2013 at 9:52
  • @IvayloStrandjev ive just got the if statemnt there to prevent nullpointer exception , the array is always {0,0},{0,0} ive put a print statement in the set method and it prints correctly however when i call it from game session its always {0,0},{0,0} ive even tried to extend ShipLocations and i stil get {{0,0}{0,0}} Commented Jan 15, 2013 at 10:03
  • @IvayloStrandjev i believe the problem lies in my gamesession class, ive debugged it and everywhere else seems to be able to get the method and reading it correctly, Im aware that im creating to shipLocations shiplocal a in client and shiplocal b in game session, however i was told that making the array static should solve this issue Commented Jan 15, 2013 at 11:35

1 Answer 1

1

Whenever you create a new instance of ShipLocations(or a subclass) the constructor is called, which in your case, reinitializes the p1sub array.

In your design, you are overusing inheritance. You should not inherit from a class just to use its methods and variables.

To store a global variable in a class:

public class ShipLocations {
 static int [][] p1sub;
 static{
  p1sub = new int[2][2];
 }
 public static void setp1sub(...){...}
 public static int[][] getp1sub(){...}
}

And then use it by class name instead of creating instances:

int [][] x = ShipLocations.getp1sub();

Though the use of global variables shoud be avoided when possible. It is considered bad design and might be a problem when reusing the code. The correct way of doing this is to have the ShipLocations object as a local variable in BattleShipsClient and set it when initializing new instance. You will then first create a common ShipLocation object and hand it to every client that should see the same array.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.