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

Popular posts from this blog

Best Programming Language to learn in 2021

The main aptitude to learn in this day and age is to realize how to compose a PC program. Today, PCs have entered in pretty much every industry. Be it the autopilot in an airplane or advanced speedometer in your bicycle, PCs in different structures encompass us. PCs are incredibly valuable for an association to scale up well. Gone are the times of pen and paper. Today, to store and access your data, you totally need PCs. The programming and engineer networks are arising at a rate quicker than at any other time. Different new programming dialects are coming up that are appropriate for various classifications of designers (tenderfoots, middle, and specialists) just as for various use cases (web application, versatile applications, game turn of events, disseminated framework, and so forth) Each amateur is astounded with the inquiry, "What programming language would it be advisable for me to learn?" Types of Programming  1. Procedural Programming Languages This programming worldv

Java - Static variable, static method and static block with example.

Java Static keyword, Static method and Static block........ Questions.......?? 1) What is static keyword , static method() , static block 2) Define static keyword , static method() , static block 3) What do you mean by static keyword , static method() , static block 4)static keyword , static method() , static block in Java ?     1)Static Keyword:- Static keyword in Java Programming language is used for memory management purpose, it indicates that a particular member belong to a type itself, rather than to an instance of that type. As we mentioned above that is used for memory management purpose it means that static keyword gets memory once in the class which help in reduction of the memory, which makes the execution speed of the program fast which is a great advantage of the static keyword. let's take example how static keyword works..... firstly we will see how memory get wasted when we didn't use the static keyword. class company {  int emp_id; 

What is Procedural Programming Language(PPL)

  Procedural Programming Language. Procedural Programming might be the primary programming worldview that another engineer will learn. Essentially, the procedural code is the one that straightforwardly trains a gadget on the best way to complete an errand in consistent advances. This worldview utilizes a direct top-down methodology and treats information and techniques as two distinct elements. In view of the idea of a methodology call, Procedural Programming isolates the program into strategies, which are otherwise called schedules or capacities, just containing a progression of steps to be done.  Basically, Procedural Programming includes recording a rundown of directions to mention to the PC what it ought to do bit by bit to complete the job needing to be done.  Key Highlights of Procedural Programming  The vital highlights of procedural writing computer programs are given underneath:  1)Predefined capacities: A predefined work is normally a guidance distinguished by a name. For the