Constructors in Java

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
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
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
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
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
- No-Arg Constructor
- Default Constructor
- 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
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
Example for No-arg Constructor
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
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
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
Syntax to create Parameterized object
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
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