In this tutorial, we will discuss Constructor in Java programming language
The constructor is similar to a method(the Special type of method) but it is not a method. It is called immediately when an instance of the object is created.
When you define constructor you should follow these criteria
When we create an object each time using the new keyword, at least one constructor is immediately called. It is called the default constructor
There are three types of constructor
Typically, default constructor doesn’t have any argument in the argument list and body of the constructor is empty. Every time you create an object using new keyword in Java, least one constructor is immediately called known as default constructor.
Syntax
Cons_name() { }
Example
class Cons{
Cons()
{
//statements
}
}
Example
Default constructor displays the default value
Program 1
class cons { public static void main(String args[]){ default_cons obj=new default_cons(); obj.display(); } } class default_cons{ int x; String name; public void display(){ System.out.println("Value of x = "+x); System.out.println("name of boy = "+name); } }
When the above code is executed, it produces the following results:
Value of x = 0 Name of boy = null
When we create an object, the default constructor is called immediately
Program 2
//create and call default constructor in Java class student_1{ student_1(){ //create a default constructor System.out.println("Student database has been created"); } public static void main(String args[]){//main method student_1 obj=new student_1();// create object for call default constructor } }
When the above code is executed, it produces the following results:
Student database has been created
A constructor which has no argument in the argument list is known as No-argument constructor. Default constructor and no argument constructor both have the same signature. However, the body of the default constructor is empty.
Example
Program 1
class Cons_Demo{ Cons_Demo()//argument list is emplty { System.out.println("This is No-argument constructor"); } public static void main(String args[]){ Cons_Demo obj=new Cons_Demo(); }
When the above code is executed, it produces the following results:
This is No-argument constructor
Program 2
class My_Sch1{ My_Sch1(){//creating default constructor System.out.println("This is my school"); System.out.println("This is default constructor"); } public static void main (String srgs[]){//main method My_Sch1 obj=new My_Sch1();//when the object created //Automatically called default constructor } }
When the above code is executed, it produces the following results:
This is my school
This is no-argument constructor
Constructor created with the argument( parameter) is called as Parameterized constructor
Program 1
class Cons_Demo1{ Cons_Demo1(int age, String name){//parameter System.out.println("my age is :"+age); System.out.println("my name is :"+name); } public static void main(String args[]){ Cons_Demo1 obj=new Cons_Demo1(34,"Jhon");//argument given } }
When the above code is executed, it produces the following results:
My age is :34 My name is :Jhon
This is a simple example for the parameterized constructor
In this example, the constructor has two parameters: age and name. We created an object named obj and passed an argument for age and name. Constructor executes using those arguments.
Program 2
class Student_data { String stu_name; int stu_id; //instance variable //parameterized constructor with two parameter Student_data(String name,int id){ //assign local variable to global variable stu_name=name; stu_id=id; } void display(){ System.out.println("Student name is : "+stu_name+" and id is :"+stu_id); } public static void main(String args[]){ Student_data stu=new Student_data("Robbert",45);//argument given stu.display(); } }
When the above code is executed, it produces the following results:
Student name is : Robbert and id is : 45
Program 3
public class MySchool{ int id; String name; //instance variavle MySchool(int stuId, String stuName ){ this.id=stuId; this.name=stuName; }//parameterized constructor with two parameter void stuInfo(){ System.out.println("Student id is "+id+" and name is "+name); } public static void main(String args[]){ MySchool obj=new MySchool(345,"Niruja"); MySchool obj1=new MySchool(45,"Laxmi"); //create object for class obj.stuInfo(); obj1.stuInfo();// call method } }
When the above code is executed, it produces the following results:
Student id is 345 and name is Niruja Student id is 45 and name is Laxmi
There are other Java language post that is similar to this post
Encapsulation in Java language
Abstract class in Java languag
Method overloading in Java language
Method overriding in Java language
Constructor overloading in Java language
difference to constructor and method in Java
Encapsulation in Java language
How to set a path to Java language
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…