简体   繁体   中英

Initializing final properties using helper private methods

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.

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.

you should call this(obj1, prop1Str, prop2Str) . This must be the first executable line in the second constructor.

Firstly, are you sure your constructors are not getting out of hand?

If you are determined (for now) on going for many constructor root, then you can call one constructor from another. Needs to be first line of constructor usig this(); syntax, which replaces implicit or explicit super(); .

public A(Object obj, String prop1Str, String prop2Str) {
    this(obj, obj, prop1Str, prop2Str);
}
public A(Object obj1, Object obj2, String prop1Str, String prop2Str) {
    // ...
}

You can, of course, transform arguments by using more complicated expressions instead of just the plain passed-in argument. For instance:

public A(Object obj, Object prop1Obj, String prop2Str) {
    this(obj, maskNull(obj), String.valueOf(prop1Str), prop2Str);
}

Constructors are there to initialise an object into a valid state. For more complex processing, you might want to add a static creation method. The method might even have a meaning name to indicate what it is doing.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM