Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:50:19+00:00 2026-06-15T02:50:19+00:00

At first I was going to make the question solely about the Image class,

  • 0

At first I was going to make the question solely about the Image class, but I wanted to make it as broadly applicable as possible.

Basically, here’s the scenario. I’m making a file for GUI constants, and in this file I’d like to have final variables for each of the Images I’m using. So my fields are declared like this UP_ARROW is:

public static final Image UP_ARROW;

Then I try to load them when the ImageIO API, like so:

static {
    UP_ARROW = ImageIO.read(new File("img/upArrow.png"));
}

Unfortunately, this isn’t valid, compilable code, because it explicitly throws IOException, which I have to deal with. So I modify it and surround it with a try/catch:

static {
    try {
        UP_ARROW = ImageIO.read(new File("img/upArrow.png"));
    }
    catch(IOException ioe) {
        //TODO
    }
}

Now I get a different compiler error. This time it says there’s a possibility that the field may not have been initialized. Okay, that makes sense. Thank you for pointing that out to me, compiler. That seems like an easy fix:

static {
    try {
        UP_ARROW = ImageIO.read(new File("img/upArrow.png"));
    }
    catch(IOException ioe) {
        UP_ARROW = null;
    }
}

Now, no matter what, the UP_ARROW must be populated with either my image or null. I’m prepared to declare victory and move on. But now I get another, unexpected compiler error:

The final field UP_ARROW may already have been assigned

… Foiled again, compiler!

Hence the question: is there any way to get around this, such that I can dynamically load final fields at runtime? Or do I declare defeat and simply make the Images non-final?

Also, an explanation as to why the compiler won’t allow this would be helpful as well. As I understand it, based on the code above, the UP_ARROW object could not have been assigned before reaching the catch{} block, because that’s what must have thrown the exception. So if the try{} executes successfully, only one assignment takes place. If it does not execute successfully, still only one assignment take place. How is that not valid?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:50:20+00:00Added an answer on June 15, 2026 at 2:50 am

    The following should do it:

    static {
        Image up_arrow = null;
        try {
            up_arrow = ImageIO.read(new File("img/upArrow.png"));
        }
        catch(IOException ioe) {
            // log the error?
        }
        UP_ARROW = up_arrow;
    }
    

    It might make sense to enclose the final assignment in a finally block .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, I know this question doesn't exactly belong here, but I'll explain later why
First I'm going to show you an image of what I'm trying to recreate
First of all, I have to say that I'm going to talk about System.ComponentModel.Component
I'm going to use Codeigniter's session data for my login system, but first I
OK, go easy on me as it's my first question here. ;) I am
quick question - I have my first view which is going to be the
This is a two part question. The first is a specific question about DataSets
I've got a class project to make a webserver in c++. Everything's been going
First I want to make it clear that this is a homework based question.
First I have to make clear what i am Trying to accomplish, it's going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.