So this more than likely has a simple answer, but i cant seem to find it. I have a 2d array in one class, what I want to do is get this array into my other three classes. Now I'm not sure but I believe I have to use inheritance, or paramater passing. If you can just point me in the right direction I'd be grateful.

The answer depends on how the four classes are related. Do any extend any others? Do the other three have a reference to the fist class instance?

Well none of the classes extend any others because if i forgot the rules, a sub class can inherit the parent correct? And a parant cannot inherit from a sub class.

@MistaGeorge: that's correct. A parent cannot inherit from subclass.

Having said that, just for the sake of availability of a 2d array you may not need an inheritance structure.

Share your class design or atleast tell us what you're trying to design so that we can guide you better.

@jalpesh_007: Dude, why are you repeating the same question as JamesCherill asked, in a different way? If it had been unintentional, then in future first read the previous posts before you give your own suggestions and try not to be redundant. Thanks.

Edited 4 Years Ago by NP-complete

Alright heres what im trying to do.

public class Numbers
public static void main(String args[])
int[][] numbers = new int[][] {{3,6,10,16,24}};

Now what i want to do is have this array in two other classes

public class Probability
public static void main(String args[]) 
public class Logic
public static void main(String args[])

There is more background to the class numbers but that should matter, do you need more information?

Edited 3 Years Ago by ~s.o.s~: formatting

The Code snippets wouldn't work for me so i took out all the brackets, Sorry.

You have defined the array inside main() of Numbers class. So the scope of that array is limited to main() of Numbers only. You would probably like to have that array as a private instance of the Numbers class and have public setters/getters to access it from other classes (perhaps Numbers should have been an Interface, but nevermind)... something like this:

public class Numbers {

    private int[][] matrix;

    public void setMatrix (int[][] matrix) {
        this.matrix = matrix;
    }

    public int[][] getMatrix (){
        return this.matrix;
    }

    // other utility methods...
    // ...

}

Now, inside Probability you do something like this:

class Probability {

    public static void main (String[] args) {

        // creating a reference of the Numbers class
        Numbers aNumber = new Numbers();

        // initialize a new matrix
        int[][] matrix = {{3, 6, 10, 16, 24}};

        // bind the newly created matrix with the Number class' matrix
        aNumber.setMatrix(matrix);

        // after the above, the matrix in Number class reference aNumber will
        // have the value {{3, 6, 10, 16, 24}}.

        // Now you can use it for your own purposes by simply calling 
        // the getMatrix() method... like this:

        int[][] matrixToBeUsed = aNumber.getMatrix();

        // Now, do whatever with this matrixToBeUsed...
        // ...

    }
}

You'll have to import the Numbers class if the Probability class is in a different package (which it should).

In case you need to have a single hard-coded matrix, you may not have a setter...
In that case,

you can alter the code as follows:

public class Numbers {

    private int[][] matrix = {{3, 6, 10, 16, 24}};

    //public void setMatrix (int[][] matrix) {
    //   this.matrix = matrix;
    //}

    public int[][] getMatrix (){
        return this.matrix;
    }

    // other utility methods...
    // ...

}

class Probability {

    public static void main (String[] args) {

        // creating a reference of the Numbers class
        Numbers aNumber = new Numbers();


        int[][] matrixToBeUsed = aNumber.getMatrix();

        // Now, do whatever with this matrixToBeUsed...
        // ...

    }
}

Same for the other classes. Hope this helps... :)

Edited 3 Years Ago by NP-complete

This article has been dead for over six months. Start a new discussion instead.