Constructor and its Types

constructor is a class method that gets automatically executed whenever class’s object is created or whenever class is initialized

– Constructor does not have any return type
– Construtctor have the same name of the class

1.Instance or Default Constructor:——–
Constructor called implicitly when we create the class instance, the modifiers can be public, private, protected, and internal or protected internal.

2. static Constructor——— will call only once when the class gets loaded in the memory without creating the instance of the class.
– It can only access the static members of the class.
– There can be only one static constructor in the class.
– The static constructor should be without parameters.

3. Parameterized Constructors:———-
will require initializing class members during instantiation
– same rules as default constructor and will have parameters

4. A private constructor is a special instance constructor. It is generally used in classes that contain static members only. If a class has one or more private constructors and no public constructors, other classes (except nested classes) cannot create instances of this class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s