Table of Contents
In this tutorial, we will discuss the concept of Constructor in Java programming language.
Constructor is a group of codes which are similar to a method. Purpose of the constructor is to create an object(instance)in a class. We may perceive constructor as a special type of method.
A constructor must follow the following rules
Java has three types of constructor
There are Three type of constructor
Generally, default constructor doesn’t have any code in the body. This means there is nothing in the body section and the default constructor does not contain any argument.
Syntax of default constructor
classname()
{
}
Example of default constructor
class cons{
cons() //this is constructor(constructor name is equalant to class name)
{
}
}
We can means, no – argument constructor looks like default constructor but it contains block of code in the body section.
Ex
class cons
{
public cons() //this is constructor(constructor name is equivalent to class name)
{
System.out.println(“This is a demo of default cons”);
}
public static void main(String args[]){
cons disp=new cons(); //creating object
}
}
In this case, this is an example of constructor. Here, we created a class named cons and created a constructor with the same name. This is a no-arg constructor in the cons class. Constructor is called when an object is created. As you can see, the following output “This is a demo of default cons” is displayed.
No-argument constructor displays default value
No-argument constructor(default constructor) provides the default value to the object 0, null. This depends on the data type.
class studetail
{
int id; //global variable
String name;
int marks;
studetail() //this is constructor name same as class name
{
System.out.println(“my name is “+name);
System.out.println(“my id is “+id);
System.out.println(“my marks is “+marks);
}
public static void main(String args[]){
studetail stu=new studetail(); //object for constructor
}
}
In the class above, we have created a constructor ,studetail class. But, it doesn’t have any parameter or any argument. So constructor displays the default value.
A constructor that has one or more parameters is called parameterised constructor.
class stu
{
int marks; //global variable
int id;
String name;
stu(int id1, int marks1, String name1) // parameter passing to constructor
{
id=id1;
marks=marks1; //assign local variable to global variable
name=name1;
}
void display(){
System.out.println(“my name is”+name);
System.out.println(“my id is”+id);
System.out.println(“my marks is”+marks);
}
public static void main (String args[])
{
stu my=new stu(24,1,”vannan”); //passing argument to parameter
my.display();
}
}
Using this keyword in the constructor in java
Program
class studentm
{
int marks; //global variable
int id;
String name;
studentm(int id, int marks, String name) //this is constructor name same as class name with parameter
{
this.id=id;
this.marks=marks;
this.name=name;
}
void display(){ //method with return type
System.out.println(“my name is “+name);
System.out.println(“my id is “+id);
System.out.println(“my marks is “+marks);
}
public static void main (String args[])
{
studentm my=new studentm(24,1,”vannan”); // passing argument for parameter
my.display();
}
}
Constructor overloading in java Method in Java Method Overloading in Java
Abstraction in C++ Abstraction in Java
Encapsulation in C++ Encapsulation in Java
Polymorphism in C++ Methodoverriding in Java
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.