So I know to declare and initialize a global variable one would have to do something like this for example:
public static int Variable = 2;
But I want to know if there is a way to initialize a global BufferedImage variable with an image from a file. I can't use the above code, since I also need to include a try/catch statement.
Anyone have any solutions to my problem?
I think that you want to use a static
bloc.
public static BufferedImage image = null ;
static
{
try {
image = javax.imageio.ImageIO.read(new File("Image path")) ; // Or whatever reader you use.
}
catch (IOException ex)
{
Logger.getLogger(Prototyper.class.getName()).log(Level.SEVERE, null, ex);
}
}
Btw, as mentioned in the first comment below, this is REALLY bad practice . Usually you want to use a Read static method, and you read the image where you need it, not by default.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.