Is it possible to declare arrays in one java file

2019-07-28 22:12发布

问题:

so how would you use that 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:

You will need to make them static. For example:

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 the same package like:

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


回答2:

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

I could have also used Singleton Principle with Composition here...but thats overkill for this..

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;


}


回答3:

Your arrays are currently local to the method main. If you move them outside of the main method (exactly as they are) then they would be visible to other classes bin the same package as they would have package access. You will need to have a reference to that class (unless you make them static). In the example below I am calling the 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 it is comon practice in java to name variables in camelCase. For example chickenRoll