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 7757003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:58:11+00:00 2026-06-01T12:58:11+00:00

I have a final member data: public final Foo foo; in the constructor, foo

  • 0

I have a final member data:

public final Foo foo;

in the constructor, foo is initialized as follow:

foo = new Foo();

Now, unfortunately, Foo’s constructor might throw an exception:

try {
    foo = new Foo();
} catch (Exception e) {
    e.printStackTrace();
}

But now compiler complains that foo might not be initialized, which is true if Foo’s constructor throws an exception. But if I put foo = null inside the catch braces, it complains that foo might have been initialized.

Foo is a third-party library that I cannot modify.

So, what’s the most graceful way of handling this?

  • 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-01T12:58:13+00:00Added an answer on June 1, 2026 at 12:58 pm
    foo = createFoo();
    
    ...
    
    
    private static Foo createFoo() {
        try {
            return new Foo();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    

    I make no comment about whether it makes sense to continue with construction of your outer object if the inner object’s constructor has failed…

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

Sidebar

Related Questions

Some of my classes have final fields that are populated from the constructor as
Is there a way to force classes in Java to have public static final
I have the following Java code: final Future future = exeService.submit( new Runnable() {
I have installed Visual Studio 2010 (final) and then Visual Studio 2008. Now I
I have a final non-static member: private final HashMap<String,String> myMap; I would like to
i have a member like this in a serializable class: private final Map<String, List<T>>
Using Doxygen 1.7.4 Sometimes I'll have a block of public member functions that belong
I read somewhere that interfaces can have member variables. Static final constants only, can
When I have a public exposed class, I usually make its member variables private,
Why can we have static final members but cant have static method in an

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.