1

I have an initializer method inside the parameterized constructor of a class. Right now this is how the class looks like

public class Example{
   private OtherClass1 otherClass1;
   private OtherClass2 otherClass2;
   private OtherClass3 otherClass3;

   public Example(){
       this(new OtherClass1(), new OtherClass2(), new OtherClass3());
   }

   public Example(OtherClass1 otherClass1, OtherClass2 otherClass2, OtherClass3 otherClass3){
       this.otherClass1 = otherClass1;
       this.otherClass2 = otherClass2;
       this.otherClass3 = otherClass3;
       initializeSomeOtherStuff();
   }

   private void initializeSomeOtherStuff(){
       // some initializer code
   }
}

I recently learned about Lombok's @AllArgsConstructor annotation which generates a parameterized constructor for a class. I want to use that here. However, I can't find a way to run the initializeSomeOtherStuff() method inside the constructor generated by @AllArgsConstructor.

@AllArgsConstructor    // Generates the parameterized constructor for Example
public class Example{
   private OtherClass1 otherClass1;
   private OtherClass2 otherClass2;
   private OtherClass3 otherClass3;

   public Example(){
       this(new OtherClass1(), new OtherClass2(), new OtherClass3());
   }

   private void initializeSomeOtherStuff(){    // How to run this function in the constructor generated by @AllArgsConstructor
       // some initializer code
   }
}

Is this even possible? If it is please tell me how.

DashwoodIce9
  • 183
  • 7
  • 2
    it isn't possible. you will to write the explicit constructor. – Eugene Jun 02 '21 at 15:51
  • 1
    Does this answer your question? [Is there any "PostConstruct" feature of lombok?](https://stackoverflow.com/questions/41156010/is-there-any-postconstruct-feature-of-lombok) This feature is in the pipeline but not yet implemented – Gautham M Jun 02 '21 at 18:33
  • I have suggested a workaround https://stackoverflow.com/a/67815143/7804477 You could try this. – Gautham M Jun 03 '21 at 03:53

0 Answers0