Home Sharing a declared array between two classes - Java
Reply: 2

Sharing a declared array between two classes - Java

hawaii Published in 2018-01-11 14:21:44Z

In good practice of OOP I am trying to separate some of my methods into two classes (not the main class).

Both classes require this particular 2D array so that the functionality of the program is executed correctly:

private char [][] board = new char[7][8];

I am unsure how to declare this within one class and be able to access it correctly within the following class so that the functionality is executed as intended.

public class DisplayBoard {

public void printBoard(){

for(int i=1; i<board.length; i++){
    for(int j=0; j<board[i].length-1; j++){
        if(board[i][j] != '\0'){
            System.out.print("| " + board[i][j] + " ");
            System.out.print("|   ");
System.out.println("  1   2   3   4   5   6   7");

My question is how do I write/rewrite the declaration of this array in one class so that it can be accessed in both classes?

The partial code from the other class is as following

public class Board {

    public void placeCounter(char player, int move){
        boolean placed = false;
        for(int i=board.length-1; i>=0; i--) {
            if(!placed) {
                if(board[i][move -1 ] == '\0') {
                    board[i][move -1] = player;
                    placed = true;

Andrew Tobilko
Andrew Tobilko Reply to 2018-01-12 09:44:06Z

Your question is incomplete but here is a method that shows how to access it.

public class Board {
    private char [][] positions = new char[7][8];

    //getters and setters


public class Another {
    Board board;

    public void printBoard(){

Vishal Raja
Vishal Raja Reply to 2018-01-15 19:31:28Z

Looking at the two classes you mentioned, it looks like you have one Board class whose responsibility is to create and maintain the actual board configurations(in this case, positions) and other class whose responsibility is to just display in a specific format.

From a design perspective, the BoardPrinter class should be implemented as a strategy with a proper interface which takes the position array as input and then this BoardPrinter class should be injected in the Board class so Board class can call a specific strategy of printing when it wants to display itself. Something like this:

interface PositionPrinter {
    public void printPositions(char[][] board);        

class BoardPrinter implements PositionPrinter {
    public void printPositions(char[][] board) {
        // Do a Board specific printing.

class Board {
    public PositionPrinter positionPrinter;
    private final char [][] positions = new char[7][8];

    public char[][] getPositions() {
        return positions;

    public void printBoard() {
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.358845 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO