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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:14:29+00:00 2026-05-27T07:14:29+00:00

I have several final properties defined in a Java class with constructor which has

  • 0

I have several final properties defined in a Java class with constructor which has all the information to initialize the properties.

public final class A {
   private final Object prop1;
   private final Object prop2;

   public A(Object someObj, String prop1Str, String prop2Str) {
       //initialize prop1 and prop2 based on information provided from someObj, prop1Str and prop2Str parameter (1)
   }
}

I would like to introduce a new constructor in class A with following signature and semantic

   public A(Object obj1, Object obj2, String prop1Str, String prop2Str) {
       //use obj1 and obj2 to initialize the someObj
       //initialize prop1 and prop2 based on information provided from someObj, prop1Str and prop2Str parameter (1)
   }

How can I reuse the code in (1)? I tried with helper private methods but Java6 gives me a compilation error since the properties of the class are final and they may not have been initialized.

EDIT:
Note that I can not call the first constructor from the second one in the first line since first I need to do some calculations and then reuse the code in question.

  • 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-05-27T07:14:30+00:00Added an answer on May 27, 2026 at 7:14 am

    You’ve found a shortcoming of final 🙂

    Java must make sure that all final fields are initialized when the constructor is finished. Because of various other limitations, that means the fields must be assigned inside the code block of the constructor.

    Workarounds:

    1. Use static helper methods (which, by design, don’t depend on the state of the class and therefore any final fields).

    2. Use a builder pattern (put all parameters in a helper class which has a build() method that returns the desired result).

    3. Don’t use final. You can get the same event by omitting setters. If you are afraid that code in the class might change the fields, move them to a new base class.

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

Sidebar

Related Questions

I have a JUnit test class in which I have several static final int
Let's say I have a model called Theme, which has several attributes setting interface
I have a base class, called NodeUpgrade , which have several child types. An
We have several wizard style form applications on our website where we capture information
I have several C and C++ projects that all follow a basic structure I've
I'm doing a webshop, and every product can have several variants. All variants are
So to stay organized, I have several javascript files, even though they all (in
I have several of these in my code which have been working fine so
So I have several select boxes like this: <select class=designer> <option>300</option> <option>3422</option> </select> <select
I have several ASP:TextBox controls on a form (about 20). When the form loads,

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.