Constructor -Same as Class Name Click Here to View Practical Example
Key Points
Key Points
- A class can have number of constructors
- A constructor doesn't have any return type, not even void
- A static constructor cannot be a parameterized constructor.
- Within a class you can create only one static constructor
Types of constructor
- Default Constructor
- Static Constructor
- Private Constructor
- Parameterized Constructor
A constructor without having any parameters is Known as default constructor
Parameterized Constructor
A constructor with at least one parameter is Known as parameterized constructor
Private Constructor
A constructor which has private access modifier is known as Private constructor. When a constructor is created with a private specifier, it is not possible for other classes to derive from this class, neither is it possible to create an instance of this class.You can define private constructor in your class when all members inside class are only static and you don't want to create the object of that class.Using private constructor neither you create a object of that class nor it can inherit by other class. If you want to create object of class that contain private constructor then you need to write a public constructor along with
private constructor.
private constructor.
Static Constructor
There is only one static constructor in the class
Key Points
There is only one static constructor in the class
Key Points
- A static constructor does not take access modifiers or have parameters
- A static constructor is called automatically to initialize the class before the first instance is created or any static member are referenced
- A static constructor cannot be called directly