Static constructors allow you to initialize static variables in a class or do other things that need to be done in a class after they first reference your code. They are called only once every time your program starts.
Static constructors are declared with this syntax and cannot be overloaded or have any parameters, because they start when your class refers to its name:
static MyClass() { }
Instance constructors are those that are called whenever you create new objects (class instances). They are also the ones you usually use in Java and most other object-oriented languages.
You use them to give your new objects their initial state. They can be overloaded and can take parameters:
public MyClass(int someNumber) : this(someNumber, 0) {} public MyClass(int someNumber, int someOtherNumber) { this.someNumber = someNumber; this.someOtherNumber = someOtherNumber; }
Call Code:
MyClass myObject = new MyClass(100, 5);
Boltclock
source share