C# static class constructor

C# has a static constructor for this purpose. static class YourClass { static YourClass() { // perform initialization here } } From MSDN: A static constructor is used to initialize any static data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is … Read more

C# constructor execution order

The order is: Member variables are initialized to default values for all classes in the hierarchy Then starting with the most derived class: Variable initializers are executed for the most-derived type Constructor chaining works out which base class constructor is going to be called The base class is initialized (recurse all of this 🙂 The … Read more

Default constructor vs. inline field initialization

Initialisers are executed before constructor bodies. (Which has implications if you have both initialisers and constructors, the constructor code executes second and overrides an initialised value) Initialisers are good when you always need the same initial value (like in your example, an array of given size, or integer of specific value), but it can work … Read more

How many constructor arguments is too many? [closed]

Two design approaches to consider The essence pattern The fluent interface pattern These are both similar in intent, in that we slowly build up an intermediate object, and then create our target object in a single step. An example of the fluent interface in action would be: public class CustomerBuilder { String surname; String firstName; … Read more

Understanding prototypal inheritance in JavaScript

To add to Norbert Hartl’s answer, SuperCar.prototype.constructor isn’t needed, but some people use it as a convenient way of getting the constructing function of an object (SuperCar objects in this case). Just from the first example, Car.call(this, name) is in the SuperCar constructor function because when you do this: var mySuperCar = new SuperCar(“SuperCar”); This … Read more

Java default constructor

Neither of them. If you define it, it’s not the default. The default constructor is the no-argument constructor automatically generated unless you define another constructor. Any uninitialised fields will be set to their default values. For your example, it would look like this assuming that the types are String, int and int, and that the … Read more

typeof for RegExp

You can use instanceof operator: var t = /^foo(bar)?$/i; alert(t instanceof RegExp);//returns true In fact, that is almost the same as: var t = /^foo(bar)?$/i; alert(t.constructor == RegExp);//returns true Keep in mind that as RegExp is not a primitive data type, it is not possible to use typeof operator which could be the best option … Read more