Constructors in Java

Constructors in Java
Constructors in Java 1

A constructor is used to create an object using a new operator. for understanding, a constructer is a piece of code used to create an object to class. constructors don’t return anything.

Syntax to create Constructor

ClassName(){
}

Constructor name should be its own class name and It should not return a value not even void, any constructer statements can be written in the body of constructer{}.

Detailed Constructor Syntax

public class ClassName
{   
     ClassName() {
         //constructer body
         //constructer statements

    }
  //class statements;
}

Working of Constructor

Constructer works while creating and object to the class,so for example student is class and we need to create a object s1 to the class student,check out the syntax below to create object

Student s1=new Student( );

new is the keyword used to create object s1 to Student class and invokes the Student( ) Constructer,now we will check the example for better understanding

example for constructor

public class Student{
    Student(){
        System.out.print("new Student is Created");
    }    

    public static void main(String args[])
    {
        Student s1=new Student();
    }
}
new Student is Created

in the above example, we created a Student class and the constructer of the student class has some print statements, in the main function we created an object s1 to the class Student, the output shows that when we created an object the class constructer is invoked, in this example “new student is created ” the string inside the constructer body, so the output will the constructer body

If there is no constructor in a class, the compiler automatically creates a default constructor.

Types of Constructors

there are totally 3 types of constructors in java

  1. No-Arg Constructor
  2. Default Constructor
  3. Parameterized Constructor

No-Arg Constructor

no-argument constructors don’t have any arguments to pass, and there will be the body of the constructer, the above basic constructer example is the No-Arg constructer example

Syntax of No-Arg Constructor

ClassName(){
}

you can check in the above syntax that there is nothing(no arguments) between the parenthesis of the constructer ClassName( )

Detailed No-arg constrcter Syntax

public class ClassName
{   
     ClassName( ) {
         //constructer body
         //constructer statements

    }
  //class statements;
}

Example for No-arg Constructor

public class Student{
    Student(){
        System.out.print("new Student is Created");
    }    

    public static void main(String args[])
    {
        Student s1=new Student();
    }
}
new Student is Created

Default Constructor

Java programs can be written without the constructer .so if we don’t use any constructer in our program then JVM provides a default constructor to the class in the runtime of the program.

Syntax of class with the default constructor

public class ClassName
{   
  //class methods
  //class or instance variables  
  //class statements;
}

default constructer provides the default values for the unintialed instance variables ,for example int value will be 0,the deafult values depend on its data type

default constructor example

public class Student{
    int age;   

    public static void main(String args[])
    {
        Student s1=new Student();
        System.out.println("default age value is "+s1.age);
    }
}
default age value is 0

Parameterized Constructor

A constructer with arguments is known as Parameterized Constructors. Parameterized Constructors are used to provide different or distinct values to different objects of the same class. the same constructer is used to provide the same values to the different objects.

Syntax to create Parameterized constructor

ClassName(arg1, arg2, arg3, .......... argN){
}

Syntax to create Parameterized object

ClassName obj=new ClassName(arg1, arg2, arg3, .......... argN);

the number of arguments in the parameterized constructer and the object arguments should be same ,and they should of same data type in the order which is present in the parameterized constructor

example for Parameterized constructor

public class Student{
    int age;  
    
    Student(int a)
    {
        age=a;
    }
    

    public static void main(String args[])
    {
        Student s1=new Student(23);
        Student s2=new Student(563);
        System.out.println("age of the student is "+s1.age);
        System.out.println("age of the student is "+s2.age);
    }
}
age of the student is 23
age of the student is 563

In the above example ,the number of arguments in the parameterized constructer is 1,and it is int data type, so in the main function while creating a object s1 and s2,we passed argument a number to indicate the age in the program, if we doesn’t provide any integer while creating the object or if we provide the unmatched data tye,compiler throws an error

Default and NO-Arg are different

So many of the programmers think that Default and NO-Arg are the same,but it’s wrong when the programmer writes a constructor then it is a No-Arg constructer even if the constructer body is empty.

but default constructer also has a constructer with empty body,but this is generated automatically by the JAva JVM in runtime,this is different from the NO-Arg constructer

public class ClassName
{   
     




  //class statements;
}
public class ClassName
{   
     ClassName( ) {
         //constructer body
         //constructer statements

    }
  //class statements;
}

Default image
Vishal Devxo
Vishal is a DevOps engineer and a Backend developer, he spends all his time for creating good tutorials with better visuals and blogging, developed some projects based on Python-Django, some hacking modules and scripts in python
Leave a Reply