Constructor overloading in Java – best practice

While there are no “official guidelines” I follow the principle of KISS and DRY. Make the overloaded constructors as simple as possible, and the simplest way is that they only call this(…). That way you only need to check and handle the parameters once and only once. public class Simple { public Simple() { this(null); … Read more

C# : assign data to properties via constructor vs. instantiating

Both approaches call a constructor, they just call different ones. This code: var albumData = new Album { Name = “Albumius”, Artist = “Artistus”, Year = 2013 }; is syntactic shorthand for this equivalent code: var albumData = new Album(); albumData.Name = “Albumius”; albumData.Artist = “Artistus”; albumData.Year = 2013; The two are almost identical after … Read more

What is the difference between init block and constructor in kotlin?

The init block will execute immediately after the primary constructor. Initializer blocks effectively become part of the primary constructor. The constructor is the secondary constructor. Delegation to the primary constructor happens as the first statement of a secondary constructor, so the code in all initializer blocks is executed before the secondary constructor body. Example class … Read more

Why can’t I overload constructors in PHP?

You can’t overload ANY method in PHP. If you want to be able to instantiate a PHP object while passing several different combinations of parameters, use the factory pattern with a private constructor. For example: public MyClass { private function __construct() { … } public static function makeNewWithParameterA($paramA) { $obj = new MyClass(); // other … Read more

Is there an alternative to bastard injection? (AKA poor man’s injection via default constructor)

As far as I understand, this question relates to how to expose a loosely coupled API with some appropriate defaults. In this case, you may have a good Local Default, in which case the dependency can be regarded as optional. One way to deal with optional dependencies is to use Property Injection instead of Constructor … Read more

What is the significance of the Javascript constructor property?

Step one is to understand what constructor and prototype are all about. It’s not difficult, but one has to let go of “inheritance” in the classical sense. The constructor The constructor property does not cause any particular effects in your program, except that you can look at it to see which function was used in … Read more

How to detect if a function is called as constructor?

NOTE: This is now possible in ES2015 and later. See Daniel Weiner’s answer. I don’t think what you want is possible [prior to ES2015]. There simply isn’t enough information available within the function to make a reliable inference. Looking at the ECMAScript 3rd edition spec, the steps taken when new x() is called are essentially: … Read more

What is an initialization block?

First of all, there are two types of initialization blocks: instance initialization blocks, and static initialization blocks. This code should illustrate the use of them and in which order they are executed: public class Test { static int staticVariable; int nonStaticVariable; // Static initialization block: // Runs once (when the class is initialized) static { … Read more

Automapper – how to map to constructor parameters instead of property setters

Use ConstructUsing this will allow you to specify which constructor to use during the mapping. but then all of the other properties will be automatically mapped according to the conventions. Also note that this is different from ConvertUsing in that convert using will not continue to map via the conventions, it will instead give you … Read more