Skip to main content

Java Constructors



Constructor

A constructor is a special type of method of a class which is automatically called when the object of the class is been created.

There are two types of constructor in Java programming language..

1) Default Constructor.

2) Parameterized Constructor.

Default Constructor

default constructor also known as No argument constructor is a constructor in which no arguments are been passed, it is used to provide values to the object like 0,null, etc. depending upon the data type.

Lets take an example of default constructor

class defaultconstructor

{

      defaultconstructor()   // default constructor

    {

        System.out.println("Default Constructor is Invoked");

    }

    public static void main(String[] args)   // main method

    {

    defaultconstructor d1=new defaultconstructor(); // object is been created

    }

}

Output

Default Constructor is Invoked

Explanation :- In the above example we have taken a class with name defaultconstructor in which we have taken the default constructor and in that block we have given a statement that default constructor is invoked , then we have taken the main method in that main method we have created the object of the class as we know that whenever the object is created constructor is automatically called, as the constructor is called the statement under the constructor block executes and we the output.

Parameterized constructor

Parameterized constructor is a constructor which has a specific number of parameters is called or known as parameterized constructor, parameterized constructor are used to provide different values to the distinct objects.

Lets take an example of parameterized constructor

class parameterized

{

    int num1,num2;

    parameterized(int n1,int n2)  // parameterized constructor

    {

        num1=n1;         // parameter passing..

        num2=n2;

    }        

    void display()   // class method

    {

        System.out.println(+num1);

         System.out.println(+num2);

    }

    public static void main(String[] args)    // main method

    {

        parameterized p1= new parameterized(2,3);   // object is been created

        p1.display();  // display method is been called through object.

    }

}

Output

2

3

Explanation :- In the above example we have taken a class with name parameterized then we have taken the parameterized constructor and passed two parameter of int data type, and passed the values in the parameterized constructor block, then we have created the display() method in which we have the printing statement to print the values. Then we have main method in which we cave created the object and passed values in the constructor after that we have called the display() method through the object.

Comments

  1. I Don't know Very Much about Java but by reading our blogs , I am learning very much, Literally, The Hard work you are doing I loved it.

    ReplyDelete
  2. Big fan sir big fanπŸ”₯πŸ”₯πŸ”₯

    ReplyDelete
    Replies
    1. Thank you sir for your kind word thank youπŸ™πŸ™

      Delete
  3. Sir I My doubt are cleared about Constructor in Java Keep Making This type blog ...Sir It will be more helpful to me..keep foing sir..

    ReplyDelete

Post a Comment