For example, it makes sense to have a default constructor for Point, resulting in Point(x = 0, y = 0) another way is to have a static constant Point.Zero. Note that immutability alone doesn't mean that there should be no default constructor. While first and last names can be defaulted to an empty string (or null, if the language authorizes null strings), what would be the birth date? The minimum authorized date? Why would you ever want an immutable person who has no name and was born in January 1 st, year 1? Imagine an immutable Person who has as properties FirstName, LastName and BirthDate. Immutable objects for which defaults make no sense. Throwing away the default constructor and having exclusively the constructor which takes the data provider as a parameter makes the class more intuitive to use, and its own code simpler. This indicates a serious problem in the design. Having a default constructor would force you to:Ĭheck in nearly every method of DataTransform that IDataProvider dataProvider field was initialized,Įxpect anyone using the class to either call the parametrized constructor, or call the default constructor and then set the data provider through an additional property or method. In order to be testable and interoperable, you have four classes implementing IDataProvider: the one for Oracle, the one for Microsoft SQL Server, the one which loads data directly from XML and finally the mock to use in unit tests. This class uses a IDataProvider which feeds DataTransform with raw data. One of the classes, DataTransform, normalizes the data from the database in order to be used in a chart. Imagine a series of classes which load statistical data from the database and display it in a chart. Reliance of a class on its field or property. There are two cases where default constructor makes little sense: private access in typical statically typed languages). In instances like these if a default constructor is required by tooling, but should not be called by anything you control or any users of your API, it's best to use the the common idiom of your language to mark the constructor as private (e.g. For instance, libraries in Java used for deserializing json/xml often require a default constructor to be invoked via reflection since those libraries will attempt to evaluate everything at runtime and can't know in advance what constructors are available to be used - so they expect a default constructor. If your class is able to provide sane defaults for all fields that comprise a valid state for objects of that class, then a default constructor is most likely a good idea.Īlso, some libraries require the existence of a default constructor for certain operations. ("I am instance initializer.Generally speaking, you should have constructors for classes that take enough parameters to properly initialize the object being created into a valid state. RoomDimension.anyStaticmethod() or before new RoomDimension()") ("I an a static initializer, If the classloader will touch your class, I will be called first! ex. But they will completely ignore the comments. Many tools, which generates, transform data will cry, nags, crash if they don't find a COMPILED "default constructor". If you don't create a default constructor, the compiler will do it, did you know? - how about your teacher? She told us that we must create a default constructor for every classĪnd I told for my teacher: Who don't know is teaching, who know is doing! Using an hex editor and this program was possible to see that there are two bytes differing, and correspond to the line numbers (used for printing stack traces), so the information is absent in this case. : in fact, the same bytecode alone doesn't guarantee that the information is not present as metadata. I'm using Java 7 with 64 bit OpenJDK, but I'd bet that it's the same with all the versions. 1: invokespecial #1 // Method java/lang/Object."":()V
0 Comments
Leave a Reply. |