java

Constructor in Java programming language

Constructor in Java programming language

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

  • Constructor must have the same name as a class name.
  • Constructor doesn’t have a return type
When Java programs are compiled, a constructor is automatically called.

Java has three types of constructor

There are Three type of constructor

  • default constructor
  • no-argument constructor
  • parameterised constructor

Default 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) 
{
}
}

no-argument(default) constructor

 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.

parameterized  constructor

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();
}
}

Program 2
class students
{
String name;
int age;
students(String name,int age){
this.name=name;
this.age=age;
}
void kristy(){
System.out.println(name);
System.out.println(age);
}
public static void main(String args[]){
students risaana=new students(“kalpana”,56);
risaana.kristy();
}
}

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

Method overloading in Java                                                Constructor overloading in Java

Exception Handling in Java

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.