I made a minimal reduced example of my problem: the Maze
class creates a 2D boolean array with the method generateMaze()
(the content of mazeArray
is irrelevant in this example). The main thread from Walker
calls that method and thereby creates this mazeArray
from the Maze
class.
I do not understand how I can call this array in Walker.walk
? I'm afraid I have a knowledge gap.
Every hint is appreciated, thank you very much.
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 class Walker {
public static void main(String[] args) {
Maze mazeObj = new Maze();
boolean[][] maze = Maze.generateMaze(2,2);
}
public void walk(Maze maze) {
// Traverse Array
}
}
There are several basic OOP mistakes here.
First of all, why do you even create an instance of the Maze
class when your generateMaze
class is static
and returns the maze as instance of boolean[][]
instead of Maze
. You probably intended to have the array as a field of the class instead and not access the array directly but via a maze instance.
Next, the walk
method is non-static and part of Walker
instances. So you would need to create an instance of that class and call the method on that instance.
You probably intended to do this instead:
public final class Maze {
// Arrays as field of maze instances
private boolean[][] mazeArray;
// return maze instance instead of array
public static Maze generateMaze(int width, int height) {
// create maze instance
Maze maze = new Maze();
// manipulate array of that maze instance
maze.mazeArray = new boolean[width][height];
for (int x = 0; x < width; x++) {
maze.mazeArray[x][0] = true;
}
for (int y = 0; y < height; y++) {
maze.mazeArray[0][y] = true;
}
// return the maze, not its array
return maze;
}
}
with a call like
Maze maze = Maze.generateMaze(2, 2);
Or even better, use a constructor:
public final class Maze {
private final boolean[][] mazeArray;
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;
}
}
}
And call it like this in your main
:
Maze maze = new Maze(2, 2);
You can still couple that with a factory method, if you really want. But the creation logic should be in a (possibly private
) constructor nonetheless:
public final class Maze {
private final boolean[][] mazeArray;
private 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;
}
}
public static Maze generate(int width, int height) {
return new Maze(width, height);
}
}
calling it like:
Maze maze = Maze.generate(2, 2);
Now, you need an instance of the Walker
class and call the method on that, giving it the maze you just generated:
Maze maze = new Maze(2, 2);
Walker walker = new Walker();
walker.walk(maze);