Requires Free Membership to View
When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to keep you informed on recent service-oriented architecture (SOA) and SOA-related topics such as integration, governance, Web services, Cloud and more.
Hannah Smalltree, Editorial DirectorIf the meaning of "properly" implies "free from developer bugs", this can only be determined by the degree of QA work completed by the builder of the class. Since Java allows exceptions to be thrown from a constructor, developers should, and usually do, throw exceptions as needed from constructors. This allows you to enclose the object construction code within a try/catch block and catch any errors or exceptions that occur.
Construction of a Java object defined by a class that extends a complex hierarchy of parent classes is an intricate process starting with the base class and continuing down through the inheritance chain. Therefore if you are attempting to make method calls inside of the constructor of a class you should make sure you know what you are doing. For example, if you attempt to call a method inside of the constructor of a class that has been extended by a subclass, and the subclass overrides the method, you will be invoking the method of the subclass. This is a dangerous practice since some of the fields of the subclass may not be initialized yet.
This was first published in January 2003