Search code examples
javamavennetbeansprivate

accessing a random variable from another class


It maybe a really easy question, but I'm confused. I have a main class and many other classes. In class DATA, one array (X_TRAIN) will be generated randomly.

public class DATA {

    public double[][] X_TRAIN;

    public DATA() throws FileNotFoundException, IOException {

         X_TRAIN = new double[2][2];
// X_TRAIN will be filled randomly
    }

    public double[][] X_TRAIN() {
        return X_TRAIN;
    }
 }

the way I call DATA class in other classes is the following:

DATA data_input = new DATA();

and the problem is that each time I call DATA, a totally new arrays (X_TRAIN) will be generated and it is not what I want. I want to have a unique X_TRAIN array. I need to call DATA because I need to have access to X_TRAIN in other classes. Maybe I'm using a wrong method to have access to X_TRAIN. My code is written in maven and the IDE is netbeans. I even tried to define X_TRAIN as private:

private double[][] X_TRAIN;

and got the following error:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - X_TRAIN has private access in com.mycompany.cpxr_main.DATA
at com.mycompany.cpxr_main.MAIN_CLASS.main(MAIN_CLASS.java:61)

Solution

  • You could have another constructor

    public DATA(double[][] trains){
      X_TRAIN = trains;
    }