Loading an image as a static one

Hello.

I’ve been trying to load an image to a static final variable and use it from another class, but I couldn’t find a way to do it.
I’m quite new to loading images in Java and displaying them…
I have used this method to load an image, but it can’t be used in static context…

Image im = new ImageIcon(getClass().getResource(“resourcename.bmp”)).getImage();

any help would be greatly appreciated…

That’s because getClass() isn’t a static method. You should write “something.getClass().getResource(…)”

Thread.currentThread().getClass().getResource(…) is often used.

That probably won’t get you the classloader you want if your code is spread across different JARs. A much better way to handle statics is to do:

ClassName.class.getResource(...);

Where “ClassName” is the name of the current class.

Yet another alternative:


Thread.currentThread().getContextClassLoader().getResource()

WFM.

Kev