0

I have been learning Java recently. I had a doubt and I am not sure if this is even possible to implement.

Suppose I have a class GameBoard and I create an array of objects,

GameBoard[][] board = new GameBoard[4][4];

Now if I need to call a non-static method of the class GameBoard, is it possible to call it as,

board.clear(); //clear the squares on the game board

or do I need to call the elements individually?

board[1][1].clear();
board[2][2].clear();

If it is possible to call board.clear(), is there a way to loop through the elements of the array to clear them inside the clear() method?

7
  • 2
    "If it is possible to call board.clear()" What happened when you tried it? Commented Jul 27, 2013 at 3:55
  • You cannot call methods on an array directly (except for the methods in class Object). You must iterate through the array using nested for loops and call clear() on each one. Commented Jul 27, 2013 at 3:56
  • trry this thing on your own ... then ask why that happened ...rather what will happen Commented Jul 27, 2013 at 4:00
  • @AndrewThompson yeah sorry about that. As I was writing the line board.clear(), it stuck me as odd. Probably should test them, get an error then post. Commented Jul 27, 2013 at 4:03
  • 1
    It's the usual way to go: try first, fail try again, fail again, think, ... and post eventually. People will be here to help after you tried :) Commented Jul 27, 2013 at 4:07

4 Answers 4

2

Yes, you'll have to clear all of them individually. Your array is just a container for many objects references that you have to access one at a time.

Hopefully, Object Oriented programming allows you to define a clear() method that can do that for you.

Wait for Java 8 and lambdas ;)

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

Comments

2

board is declared as array, not object of GameBoard

So it contains object of GameBoard

when you do this board it is an array. when you do board[0][0] this, type is GameBoard, so you can only call by accessing cells of the array.

Comments

1

You're initializing a 2-dimensional array of GameBoard instances, 16 in total, with the following line:

GameBoard[][] board = new GameBoard[4][4];

This board array of arrays will contain null values for all elements, until you initialize them, e.g.:

board[0][0] = new GameBoard();

Or, in a loop:

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
        board[i][j] = new GameBoard();
    }
}

You're right that you can't call clear() on board directly, clear() is not something you can call on an array. You'd want to call board[i][j].clear() or in a similar loop (but only after you've initialized them, or you'll try to call something on null, and get a NullPointerException).

2 Comments

Ha thanks Matthieu, I just approved your edit of my code while reviewing the review queue (lol I stumbled on my own answer)!
Hehe no problem, I'm a typo freak :) I had to add a couple extra () to match the 6-char limit though ;)
0

board is an array of Object gameBoard. you need to first qualify array and then can use static refrence

Comments

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.