1

I have the following method in one of my classes called Board. Board has an array of 120 Squares, which is another class from my program.

public class Board{

private Square[] square = new Square[120];
...

Each Square has an int row and an int column.

public class Square extends JButton{
public int row;
public int column;
...

The method itself is supposed to figure out what the row and column is for every Square inside

    void setSquares() {
    int ones;
    int tenths;
    Square s = new Square();
    Insets squareMargin = new Insets(5, 5, 5, 5);
    s.setMargin(squareMargin);
    for (int i = 0; i < square.length; i++){

        ones = getNdigit(i, 1);
        tenths = getNdigit(i, 2);

        //set row && set column
        if ((tenths >= 2 && tenths <= 9) && (ones >= 1 && ones <= 8)){
            s.row = tenths - 1;
            s.column = ones;

        } else{
            s.row = 0;
            s.column = 0;
        }
        square[i] = s;
        System.out.println(square[0].toString());

    }

So at the end of the method, I expect that square[34] has a row of 2 and a column of 4. However, the actual result is always the same as where the for loop ended (square[34] has a row and column of 0). If the for loop was changed to

for (int i = 0; i < 55; i++){

then square[34] has a row of 4 and a column of 4.

2
  • 1. You forgot to ask a question. 2. What value is not changing correctly? Provide sample input and output (both expected and actually received) Commented Mar 6, 2017 at 8:18
  • The value of both int and columns wasn't changing correctly, My question was why. I provided sample data in the last paragraph. Commented Mar 6, 2017 at 8:32

1 Answer 1

2

You are creating only one instance of Square and use it throughout the for loop. Move the instantiation inside the for loop so each instance stored will be different.

To answer your question in the comment:

Square s = new Square();

Allocates some space in memory to store the Square instance (where you can set values to its members). so now s references that space in memory.

square[i] = s;

so now square[i] references that same space (thus having the same member values). so for every i all square[i] references to the same place (the same instance of Square). But if you allocate s one each iteration then s will reference a new square and each square[i] will reference a different Square instance

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

2 Comments

This is all I needed to do. I'm amazed. Do you mind if I ask why does this happen? The value of s changes in every loop, why does square[i] change with it?
This makes so much sense now. I've been banging my head against the wall for hours now, and you just came in like a dose of Vicodin. I can't be grateful enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.