Search code examples
javaarraysmultidimensional-arraysingletonmethod-call

I don´t understand why this call does not work (Java)


The class Maze instanciates an 2D boolean-Array with the method: generateMaze(int width, int height) which has to be used in Walker. The method-call to create the boolean[][] happens at the main of the Walker class

My aim is to call the boolean[ ][ ]-Array with walk(Maze maze),the input for the method has to be the Maze-Object, not the actual boolean[ ][ ]. I do not understand how to call it, there has to be a way to call it with an object instance of an Maze-Object.

public final class Maze{

    public static boolean[][] generateMaze(int width, int height) {

        boolean[][] mazeArray = new boolean[width][height];

        for( int x = 0; x < width; x++ ) {
            mazeArray[x][0] = true;
        }
        for( int y = 0; y < height; y++ ) {
            mazeArray[0][y] = true;
        }
        return mazeArray;
    }

    public boolean[][] getBooleanArray() {
        return generateMaze(2,2);
    } 
}
public class Walker {

    public static void main(String[] args) {
        boolean[][] maze = Maze.generateMaze(2,2);

        Walker walker = new Walker();
        Maze mazeObj  = new Maze();

        walker.walk( mazeObj );
    }

    public void walk(Maze maze) {
        // This call doesnt work. Why?
        System.out.println( mazeObj.maze[0][0] );
    }
}

Solution

  • Pretty much everything is wrong with the provided code, as far as what I think you are trying to achieve boolean[][] maze should be a instance variable for your maze objects, getBooleanArray() is completely redundant, and the work you want done when initializing a new object should be under the constructor. Additionally the final declaration for Maze class does't really make sense in this context, but I can't comment on that as I don't know about the rest of your program.

    A possible fix with explanations is provided below:

    public class Maze{
    
        /** Declare a new mazeArray instance variable (variables that are
         *  attributes of specific instances of the class). It is common to
         *  declare the instance variables private and manage access via
         *  public accessor methods. See below for the accessor method.
         */
        private boolean[][] mazeArray;
        
        /** Move the contents of the generateMaze to the constructor,
         *  constructors are called when a new object is being instantiated.
         *  (basically when you call new <ClassName>()), you do not have to
         *  return the mazeArray anymore as it will be accessed via an
         *  accessor method.
         */
        public Maze(int width, int height) {
    
            mazeArray = new boolean[width][height];
    
            for( int x = 0; x < width; x++ ) {
                mazeArray[x][0] = true;
            }
            for( int y = 0; y < height; y++ ) {
                mazeArray[0][y] = true;
            }    
        }
    
        /** This is the accessor method that will be 
         *  used by the outside world to access
         *  the mazeArray field
         */
        public getMazeArray() {
            return mazeArray;
        }
    }
    

    also the Walker class is faulty in the way that in terms of object oriented programming.

    public class Walker {
    
        public static void main(String[] args) {
    
            /* Calling new Maze() create a Maze object with the private
             * boolean[][] mazeArray that you can access with the same
             * object's getMazeArray() method.
             */
            Maze maze = new Maze();
    
            walk(maze);
    
        }
        /** I don't know if you will have multiple Walker objects in your
         *  program, depending on that decision you might want to make this
         *  method non-static but for now this will suffice
         */
        public static walk(Maze maze) {
    
          boolean[][] mazeArray = maze.getMazeArray();
    
          System.out.println(mazeArray[0][0]);
    
        }
    }
    

    Also you don't seem to be using Java properly, and as someone who was at the same place a couple of years ago I would recommend Head First Java as a good place to start reading. Also you might want to linearize your 2D array to prevent later performance issues depending on where your project is headed.