I am working on an assignment in Java and having a 2D array. I have created multiple classes and at the moment I am creating the array in each and every class and was wondering if there is an easy way to create the array only once in the whole program and call it in different classes. I have created a 2D array and I don't want to use ARRAYLIST (as per my knowledge ARRAYLIST is used when there is no fixed size of array but in my case I know my ARRAY is 5x5).
Here is a basic example of my code:
public class accounts{
static int option=0;
static String[][] resultCard = new String[][]{
{ "", "A", "B", "C", "D", "E"},
{ "Account1", "1", "2","3","4","5" },
{ "Account2", "6", "7", "8", "9", "10"},
{ "Account3", "11", "12", "13", "14", "15"},
{ "Account4", "16", "17", "18", "19", "20"},
{ "Account5", "21", "22", "23", "24", "25"}
};
public static void method1() {
//I have entered some code here
}
Then I have created another class as follows:
public class customers{
static int option=0;
static String[][] resultCard = new String[][]{
{ "", "A", "B", "C", "D", "E"},
{ "Account1", "1", "2","3","4","5" },
{ "Account2", "6", "7", "8", "9", "10"},
{ "Account3", "11", "12", "13", "14", "15"},
{ "Account4", "16", "17", "18", "19", "20"},
{ "Account5", "21", "22", "23", "24", "25"}
};
public static void method2() {
//I have entered some code here
}
I have another class as follows:
public class staff{
static int option=0;
static String[][] resultCard = new String[][]{
{ "", "A", "B", "C", "D", "E"},
{ "Account1", "1", "2","3","4","5" },
{ "Account2", "6", "7", "8", "9", "10"},
{ "Account3", "11", "12", "13", "14", "15"},
{ "Account4", "16", "17", "18", "19", "20"},
{ "Account5", "21", "22", "23", "24", "25"}
};
public static void method1() {
//I have entered some code here
}
public static void method1() {
//I have entered some code here
}
I want to know that instead of creating the array in each class, is it possible that I create one global array in only 01 class and call the array in different classes.
MyClass.resultCard.public class Staff)