1

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).

4
  • 1
    Is that your variables require for a treatment?? if no you can use file properties! Commented Apr 24, 2017 at 15:42
  • They should be consistent across all these. I'd have to know more about the relationship between these classes to answer. You should be DRY: Let one of those classes be the steward for those values and give read access to all other clients. Commented Apr 24, 2017 at 15:44
  • @duffymo Actually, I use those variables for the FOR statements. Commented Apr 24, 2017 at 15:49
  • You can still get them from the one source of truth of all the other objects have a reference to the owner of the loop limits. This is an easy problem. Commented Apr 24, 2017 at 16:03

6 Answers 6

1


At first. Please Post the other classes.
Seccondly you have to write getter and setter for the Variables.

public int getGrid_size(){
 return grid-size;
}
private int getGrid_subsize(){
   return grid_subsizes;
}

Yes its right you should not use static Variables. But if you would You can create an Utility Class like:

public class Utility{
    public static SudokuGenerator generator;
}

You Can access the Variable with:

int x = Utility.generator.getGrid_Size();
Sign up to request clarification or add additional context in comments.

12 Comments

It is not recomended to create public static Variables by Java
This is why I didn't want to create static variables. But it looks awful with all that variables in the constructors and I only need them for FOR loops.
Yes Thanks for the Code
You have dublicated all the classs at least SudokuGrid SudokuSolver SudokuGenerator are compltely the same... where you wuld acess the Sudoku Grid class Variables?
In the Main clas a create instance of SudokuGame and in SudokuGame I create instance of SudokuGrid. In the SudokuGrid class I create instance of SudokuSolver and SudokuGenerator and I pass the variables like constructor arguments.
|
1

Initiate a class like below

public class gridSizes{
    public static int grid_size = 350;
    public static int grid_subsize = 30;
}

Then you can retrieve the value anytime by gridSizes.grid_size or gridSizes.grid_subsize

3 Comments

If the class modifier is a problem make it private and use getters and setter.when doing so we must create an object to the class and then use the object to access it
Or you use the logical choice and make them final, but that's up to you.
I don't want to create an instance of the class everytime I need the variables, because the variables will have always the same value (in every class)
0

You can use it like that, I see no problem with it. I do not see any problem either with using static variables at this point (I assume those variables are constants), because using static variables would make it a global variable known through all classes, which is here what you want and which is one of the purposes static is meant for. People call static mostly bad , because of the main method. In the static main method you cannot reference to a non static method, so they go on and on with declaring everything static. The meaning of static is that you do not need to instantiate an Object to access this variable or method. You can always access it by calling the class name dot variable or method name.

1 Comment

@Masheshvara At the moment I need them like constants (9x9 grid). But I was thinking, I may create 6x6 sudoku too, so the variable could change to 9 or 6.
0

i'd use private static variables with public static Getters.

Your variables always must have the same value for all ojects ... thats what static variable were designed for.

1 Comment

They have the same value for all objects.
0

My thought is as follows

  1. I believe classes SudokuGrid,SudokuSolver and SudokuGenerator all are interdependent.

  2. So why not define SudokuGame as an Outer Class and SudokuGrid,SudokuSolver and SudokuGenerator as nested Inner Classes , define private int grid_size; private int grid_subsize; in the SudokuGame class, then access both variable in the inner classes as needed be.

  3. You can also create nested static classes , but nested static classes can only access static content of the outer class.

Hope that helps.

Comments

0

if you want to use grid_size and grid_subsize in other classes you need to define your variables public, but you cant use different Object to access the same value of grid_subsize and grid_size if you don't want to use static variables then here is my solution

here is your public class

public class Stackoverflow {

public Stackoverflow(){
    SudokuGenerator a = new SudokuGenerator(1,2);
    newclass b= new newclass(a);
}
public static void main(String[] args) {
   new Stackoverflow();
}    
}

after you initialize your variables you can simply pass the object to any other classes so you can fetch the same data.

    class SudokuGenerator {
  public int grid_size;
  public int grid_subsize;

  public SudokuGenerator(int grid_size, int grid_subsize) {
    this.grid_size = grid_size;
    this.grid_subsize = grid_subsize;
   }  
 }

class newclass{
    public newclass(SudokuGenerator a){
        System.out.println("grid_size and grid_subsize in newclass " + a.grid_size + ", "+a.grid_subsize);
    }

2 Comments

Shouldn't those variables be private?
well if you define them private then you have to define a Getter method to return the variable instead, I myself prefer to use static variables though

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.