Is it possible to declare arrays in one java file and use them in another java file if they are all in one package

so how would you use this with a completely different java file, but in the same package

public static void main (int[] args) { int [] HotDog = {18,8,10,0}; int [] ToastedChicken = {25,8,17,0}; int [] ToastedSteak = {30,8,22,0}; int [] ToastedEggT= {20,8,6,6}; int [] ToastedSteakE={36,8,22,6}; int [] ChickenRoll = {25,8,17,0}; int [] SteakRoll = {30,8,22,0}; int [] EggTomato = {20,8,6,6}; int [] CheeseTomato = {20,8,6,6}; int [] steakEgg = {36,8,22,6};` 

IE here

 if (contents == "Hot Dog") {jLabel2.setText(HotDog[2]); } 
+1
source share
3 answers

You will need to make them static. For instance:

 final class MyConstants { static final int[] HotDog = {18, 8, 10, 0}; static final int[] ToastedChicken = {25, 8, 17, 0}; static final int[] ToastedSteak = {30, 8, 22, 0}; static final int[] ToastedEggT = {20, 8, 6, 6}; static final int[] ToastedSteakE ={36, 8, 22, 6}; static final int[] ChickenRoll = {25, 8, 17, 0}; static final int[] SteakRoll = {30, 8, 22, 0}; static final int[] EggTomato = {20, 8, 6, 6}; static final int[] CheeseTomato = {20, 8, 6, 6}; static final int[] SteakEgg = {36, 8, 22, 6}; private MyConstants() { // Prevents users of this class from instantiating a useless instance of // this class, since all members are static. } } 

Then you can use them in any class in one package, for example:

 if (contents.equals("Hot Dog")) { jLabel2.setText(MyConstants.HotDog[2]); } 
+5
source

And I will try to make those Array as static , so the whole class trying to access these arrays gets the same array ...

I could also use Singleton Principle with Composition here ... but that's too much for that.

 public class Test{ static int [] HotDog = {18,8,10,0}; static int [] ToastedChicken = {25,8,17,0}; static int [] ToastedSteak = {30,8,22,0}; static int [] ToastedEggT= {20,8,6,6}; static int [] ToastedSteakE={36,8,22,6}; static int [] ChickenRoll = {25,8,17,0}; static int [] SteakRoll = {30,8,22,0}; static int [] EggTomato = {20,8,6,6}; static int [] CheeseTomato = {20,8,6,6}; static int [] steakEgg = {36,8,22,6}; } public class AccessIt{ Test.HotDog; } 
+1
source

Your arrays are currently local to the main method. If you move them outside the main method (just like they are), they will be visible to other classes in the same package, since they will have access to the package. You will need to have a reference to this class (unless you make them static). In the example below, I call class X

 int [] HotDog = {18,8,10,0}; int [] ToastedChicken = {25,8,17,0}; int [] ToastedSteak = {30,8,22,0}; int [] ToastedEggT= {20,8,6,6}; int [] ToastedSteakE={36,8,22,6}; int [] ChickenRoll = {25,8,17,0}; int [] SteakRoll = {30,8,22,0}; int [] EggTomato = {20,8,6,6}; int [] CheeseTomato = {20,8,6,6}; int [] steakEgg = {36,8,22,6};` public static void main (int[] args) { jLabel2.setText(new X().HotDog[2]); //will return 10 } 

Also in java, you can name the variable names in camelCase. For example, chickenRoll

0
source

All Articles