Constructor 
Constructor is a special non static member because :
    1. Constructor name must be as same as classname.
    2. Constructor is similar to method but it doesn't have any return type.

Why do we need constructor ?
To store all non static members in object.

Program:

  1. class P1
  2. { 
  3. int a=10;
  4. P1( )
  5. {
  6. }
  7. void dell( )
  8. {
  9. }
  10. public static void main(String [ ] args)                                                
  11. {
  12. P1 ref = new P1( );
  13. }





Difference between  Method and Constructor

Method

Constructor

1)Method should have any return type.

1)Constructor doesn’t have any type.

2)Method should have a name.

2)Constructor have only classname.

3)It should be called directly and with the help of object reference.

3)Constructor should be called using a new keyword.

4)Method should be static and non-static.

4)Constructor should be only non-static.

5)Method is created by the programmer.

5)Constructor will be created by the compiler implicitly.



Types of constructor
  1. No-argument constructor
    • The constructor which does't having any formal argument.
  2. Parameterized constructor
    • The constructor which has formal argument.
NOTE- 

1) If the class is not having any constructor by default compiler will add no argument constructor during compile time is known as default constructor.

2) Every constructor will have three things 
    1. PLI=(Pre Loading Instructions==To load/ store nonstatic members into obect
    2. IIB=(Instance Initializer Block=non-static Block)
    3. UWS=(User Written Statement)