CONSTRUCTOR

Constructor

It defines a block with the same name as the name of the class to which it belongs.
  1. Constructor syntax doesn't include a return type,since constructors never return a value.
  2. Constructor is invoked whenever object is created to the corresponding class.
  3. By using constructor, we can construct initial state of the object before completing the object creation.

Type of Constructor

javapadho
Type of Constructor

Program
class Person{
        String name;
        int age;
        double height;
        double weight;
        /* initializing through constructor */
        Person(int age,double height,double weight)
        {
        /* age=age; if we give like this jre will get ambiguity error  like which variable value we are assigning to which variable. */
        this.age=age;
        this.height=height;
        this.weight=weight;
        /* if we use same name for both local variables and global variables then we can differentiate the global var by using 'this' operator. */
        }
        }
        
class PersonManager { public static void main(String []args) { /*Person p1=new Person(23,6.2,56.0); we are initializing attr through constructor.*/ Person p2=new Person(25,5.2,60.0); System.out.println("Name:"+p1.name); System.out.println("Age:"+p1.age); System.out.println("Height:"+p1.height); System.out.println("Weight:"+p1.weight); System.out.println("Name:"+p2.name); System.out.println("Age:"+p2.age); System.out.println("Height:"+p2.height); System.out.println("Weight:"+p2.weight); } }

                                name:null
                                age:23
                                height: 6.2
                                weight: 56.0
                                name:null
                                age:25
                                height:5.2
                                weight:60.0

                            
	

Default Constructor:

javapadho
Default Constructor

A.java file contains empty block with the class name. In case of A.class the file contains No args constructor, it means while compilation time, compiler creates default constructor with No Args if there is no constructor inside the class.

If there is a constructor like A(String s) or A(int i) or A(char c) inside the A.java file then the compiler won't create default constructor with no args as it already contains a constructor inside the class.

Constructor Signature Rules:

  1. Name of constructor should be same as classname.
  2. List of the Arguments.
  3. Type of the Arguments.
  4. Order of arguments.

Constructor Overloading :

  1. Keeping more than one constructor with different signature inside the same class.
  2. we can define 'n' number of constructors or as many as constructors inside one class.
  3. If we want to define more than one constructor inside one class then every constructor should have different signature or unique signature.
  4. While creating an Object, minimum 2 constructors will be called.
  5. Main purpose of constructor is initializing the state of the Class Object.
javapadho
Different Type Constructor

                                class Rectangle
                                {	int l,b;
                                Rectangle()
                                {   l  = 20;
                                b  = 20;
                                }
                                Rectangle(int l, int b)
                                {   this.l = l;
                                this.b = b;
                                }
                                Rectangle(String msg)
                                {  System.out.print(msg+" ");
                                }
                                int arae()
                                {	return l*b;
                                }
                                }

                                public class ConstructorOverloading
                                {
                                public static void main(String[] args) {
                                Rectangle rect1msg  = new Rectangle("Area using Default constructor ");
                                Rectangle rect1  = new Rectangle();
                                System.out.println(rect1.arae());
                                Rectangle rect2msg  = new Rectangle("Area using int type constructor ");
                                Rectangle rect2  = new Rectangle(5,8);
                                System.out.println(rect2.arae());
                                }
                                }
                            
	
Output :
Area using Default constructor 400
Area using int type constructor 40

class College
                                {
                                College(int i)
                                {  System.out.println(i);	}
                                College(float f)
                                {  System.out.println(f);	}
                                }
                                class University
                                {	public static void main(String args[])
                                {
                                /* College c=new College(); */
                                /* Here Compilation fails because  inside College class there is */
                                /* no default constructor.*/
                                /* If  College class don't have any constructor*/
                                /* then compiler will put the default constructor.*/
                                /* But the class have int & float type constructors. */
                                College c1=new College(10);
                                College c2=new College(10.25f);
                                }      }
                            
Output :
10
10.25