Ben
Ben

Reputation: 317

How to call in a variable from an object?

I'm not quite sure how to word this question, so here it goes. In one class, I have this:

public class Board {
//instance variables

private char BLANK = '_';
private char[][] board;

public Board(){
    int SIZE = 9;
    char[][] board = new char [3][3];
    for (int i=0; i<board.length; i++){
        for (int j=0; j<board[i].length; j++){
            board[i][j] = BLANK;
        }
    }
}
}

And in another (the 'class' tag is ommitted):

public int getComputerSpot(){
    int nbr = r.nextInt(Board.SIZE)+1;
    while (!board.isAvailable(nbr)){
        nbr = r.nextInt(Board.SIZE)+1;
    }

    return nbr;
} 

It cannot find Board.SIZE; it says it cannot find the variable. What's missing, or how do I go about making sure it can find Board.SIZE? Thanks!

Upvotes: 0

Views: 261

Answers (4)

takendarkk
takendarkk

Reputation: 3443

Since you declare and initialize SIZE inside the constructor, that is the only place it is visible. You would need to declare it as a class variable instead. To access it in the way you show (Board.SIZE) you would need to make it a public variable. If you want it private like the others you would need to make a getter method like getSize() which would then return that private variable. Also, it looks like you are trying to get the variable directly from the class itself, not from an object of that class. In that case, you would also need the variable to be static.

Public version

public class Board {
    public static int SIZE = 9

    public Board() {
        // remove "int SIZE = 9" from your constructor
    }
}

Private version

public class Board {
    private static int SIZE = 9

    public Board() {
        // remove "int SIZE = 9" from your constructor
    }

    public int getSize() { return SIZE; }
}

Side note:

You have declared a variable at the class level here private char[][] board; and then inside your constructor you are doing it again here char[][] board = new char [3][3];. These are going to be 2 different variables which is almost certainly not what you want. In order to not create this confusion you only need to initialize board in the constructor, not re-declare it. In your constructor change

char[][] board = new char [3][3];

to just

board = new char [3][3];

Upvotes: 0

Gren
Gren

Reputation: 1854

In addition to the previous answers: Have a look on the class-variable board. In the constructor, the class-var is locally hidden by the local defined variable board. So, if you access the class variable later, you may become confused because the class variable is uninitialized.

Upvotes: 1

user384842
user384842

Reputation: 1975

Try:

public class Board {
    // Static variables (also known as class variables)
    static int SIZE = 9;

Now you can refer to Board.SIZE from other classes.

Check out this Oracle tutorial about 'static variables' or 'class variables' for more information: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Upvotes: 1

DWilches
DWilches

Reputation: 23015

The SIZE variable will only exist inside the Board constructor, it will cease to exist when the constructor finishes, and you can only access it from the constructor lines after you declared it.

So what you need is to declare SIZE as an attribute of the class, not as a local variable.

Upvotes: 1

Related Questions