zrlw opened a new issue, #15600: URL: https://github.com/apache/dubbo/issues/15600
### Pre-check - [x] I am sure that all the content I provide is in English. ### Search before asking - [x] I had searched in the [issues](https://github.com/apache/dubbo/issues?q=is%3Aissue) and found no similar feature requirement. ### Apache Dubbo Component Java SDK (apache/dubbo) ### Descriptions **Key Reasons to Avoid Method Calls in Constructors** 1. Incomplete Object Initialization: When a constructor calls other methods, those methods may execute before the object is fully initialized, leading to potential NullPointerExceptions or inconsistent states. 2. 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. 3. Reduced Code Clarity: Constructors should focus solely on initialization. Adding method calls makes the code harder to understand and maintain. 4. 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() { initialize(); } } public class BBB extends AAA { private final Object myFinal = new Object(); public BBB() { super(); } public void initialize() { new Thread(() -> { // The assertion might fail because myFinal isn't initialized during running BBB construction. Assertions.assertNotNull(myFinal); }).start(); } } ``` **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 [During the initialization process of AbstractServer, the doOpen() method publishes the uninitialized internal state to the external thread, resulting in: "this.dubboChannels" is null](https://github.com/apache/dubbo/issues/14848) ### 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 - [x] I agree to follow this project's [Code of Conduct](https://www.apache.org/foundation/policies/conduct) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
