-
Notifications
You must be signed in to change notification settings - Fork 26.6k
Open
Labels
help wantedEverything needs help from contributorsEverything needs help from contributorstype/bugBugs to being fixedBugs to being fixed
Milestone
Description
Pre-check
- I am sure that all the content I provide is in English.
Search before asking
- I had searched in the issues and found no similar feature requirement.
Apache Dubbo Component
Java SDK (apache/dubbo)
Descriptions
Key Reasons to Avoid Child Method Calls in Constructors
- Incomplete Object Initialization: When a constructor calls other methods, those methods might execute before the object is fully initialized, leading to potential NullPointerExceptions or inconsistent states.
- Inheritance Issues: If the called method is overridden by a subclass, the subclass version will execute before the subclass constructor completes, violating the expected initialization order.
- Reduced Code Clarity: Constructors should focus solely on initialization. Adding method calls makes the code harder to understand and maintain.
- Testing Difficulties: Methods called during construction make unit testing more complex, as you can't test the constructor independently from those methods.
Better Alternatives
Instead of calling methods in constructors:
- Initialize fields directly
- Use factory methods
- Implement lazy initialization
- Apply the Initialization-on-demand holder idiom for singletons
Example of Problematic Code
public abstract class AAA {
public AAA() {
System.out.println("Build AAA");
initialize();
}
protected void initialize();
}
public class BBB extends AAA {
private final FFF myFinal = new FFF();
public BBB() {
super();
System.out.println("Build BBB");
}
@Override
public void initialize() {
System.out.println("Run initialize");
new Thread(() -> {
// The assertion might fail because myFinal isn't initialized during running BBB construction.
Assertions.assertNotNull(myFinal);
}).start();
}
}
public class FFF {
public FFF() {
System.out.println("Build FFF");
}
}
Help Wanted
Keeping constructors simple and focused on field initialization to create more maintainable and reliable code that properly follows object-oriented principles.
Related issues
Are you willing to submit a pull request to fix on your own?
- Yes I am willing to submit a pull request on my own!
Code of Conduct
- I agree to follow this project's Code of Conduct
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
help wantedEverything needs help from contributorsEverything needs help from contributorstype/bugBugs to being fixedBugs to being fixed
Type
Projects
Status
Todo