0

In the below example:

class Foo {
    public static String[] listofimages = new String[]{};

    public static void main(String[] args) {

        for(int i = 1; i <= 3; i++) {
                   listofimages[i] = //some image that I am adding to it
        }

    }
}

I am trying to access the newly modified listofimages variable in another class, Foobar (see below):

class Foobar(){
    public void someClass{
        Foo.listofimages //trying to access the newly modified listofimages variable with values in it
}

The above code is not working for me. I am confused on how to go about accessing the newly modified static variable listofimages, in a class that it is not declared in, class Foobar. Any help would be very much appreciated, thank you! :)

2
  • Note that the scope of static variables is Class, i.e as long as the Class stays static variables stay. Commented Dec 26, 2014 at 9:01
  • Foobar is a seperate Class right..? Commented Dec 26, 2014 at 9:27

3 Answers 3

3

If you are initializing the static array as shown, i.e. - listofimages = new String[]{}, it is an empty array and listofimages[i] will always throw an exception. You should initialize it with a positive length and only assign to valid indices of that array.

As for accessing that array from a different class, Foo.listofimages should work (though Foo class will have to be public in order to be visible to classes that belong to other packages).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your thoughts! however I have tried initializing it with a positive length and only assigning values to valid indices, and the problem is persisting. It seems like the value "Foo.listofimages" that is being accessed in Foobar is the initialized value without the modification, or no values at all in listofimages
@user3777343 Is the code accessing the static array running in the same thread as the code that modifies that array?
1

When you refer to a class for the first time statically, all of its static variables are initialised. In this case, referencing Foo has the effect of initialising the array. However, it is empty (as in has zero length).

Even though you add elements in the main method, it is never called before you reference listofimages. Hence, it is still empty and referencing an index in an array that does not feature the index throws an Exception.

To fix the problem you would need to either:

  • initialize the array to a specific size,
  • make sure main is called before you access the array,
  • initialize the array statically, including all its elements.

However, seeing as you seem to be new to Java, I would like to point out some quirks with your design. If you have a static variable then you express that the variable should be static, as in having the same unique value for all instances of your class. In essence: for all Foo instances, listofimages should always have the same contents and all references should point to the same instance. When starting with Java, it is unlikely that you need this construct. May I suggest you simply declare it as public? Then you could move the initialisation into the constructor of Foo and you would avoid the problem you face altogether.

Comments

1

At the time of class loading all the static variables are being initialized...in this case the array listofimages is also initialized (though its empty) . Now when you call the method someClass of class Foobar you are trying to access the initialized but empty listofimages

Solution:

Either make sure that the main method is called before you access the someClass method so that your array can be populated or what you can do is initialize the array like:

public static String[] listofimages = {"a","b"};

1 Comment

@user3777343 Please do select the answer if it helped

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.