I have to variables grid_size and grid_subsize and classes SudokuGrid, SudokuGame, SudokuSolver and SudokuGenerator.
The varibles are first initiated in SudokuGrid and after that I need to use them in the remained classes. At the moment I am passing the variables like arguments in the constructors of the classes. Something like:
public class Main extends Application {
public static void main (String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Sudoku");
int grid_size = 9;
int grid_subsize = 3;
SudokuGame sudokuGame = new SudokuGame(grid_size, grid_subsize);
}
}
public class SudokuGame {
public SudokuGame (int grid_size, int grid_subsize) {
SudokuGrid sudokuGrid = new SudokuGrid(grid_size, grid_subsize);
}
}
public class SudokuGrid {
private int grid_size;
private int grid_subsize;
public SudokuGrid (int grid_size, int grid_subsize) {
this.grid_size = grid_size;
this.grid_subsize = grid_subsize;
}
}
public class SudokuSolver {
private int grid_size;
private int grid_subsize;
public SudokuSolver (int grid_size, int grid_subsize) {
this.grid_size = grid_size;
this.grid_subsize = grid_subsize;
}
}
public class SudokuGenerator {
private int grid_size;
private int grid_subsize;
public SudokuGenerator(int grid_size, int grid_subsize) {
this.grid_size = grid_size;
this.grid_subsize = grid_subsize;
}
}
But it doesn't look very well.
My second idea is to use them like public static variables, but many people say that public static variables are "EVIL".
So, what is the best way to use one/more variables in more classes?
Edit: What about creating in the SudokuGame class public static methods for grid_size and grid_subsize (I don't need to create everytime a new instance).