0% found this document useful (0 votes)
44 views

Constructors Java

Constructors in Java are special methods used to initialize objects that are called when an object is created. There are two types of constructors: the default no-arg constructor that provides default values if no other constructor is present, and parameterized constructors that can provide custom initialization values. Constructors must have the same name as their class and cannot specify a return type.

Uploaded by

young blood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Constructors Java

Constructors in Java are special methods used to initialize objects that are called when an object is created. There are two types of constructors: the default no-arg constructor that provides default values if no other constructor is present, and parameterized constructors that can provide custom initialization values. Constructors must have the same name as their class and cannot specify a return type.

Uploaded by

young blood
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Constructors

1. A constructor in Java is a special method that is used to initialize


objects.
2. The constructor is called when an object of a class is created.
3. It can be used to set initial values for object attributes

Rules for creating Java constructor


There are two rules defined for the constructor.
1. Constructor name must be the same as its class name
2. A Constructor must have no explicit return type
3. A Java constructor cannot be abstract, static, final, and synchronized

Types of Java constructors


There are two types of constructors in Java:
1. Default constructor (no-arg constructor)
2. Parameterized constructor

Java Default Constructor or no- arg Constructor


A constructor is called "Default Constructor" created by compiler and it doesn't have any
parameter.

Syntax of default constructor:


class_name()
{

}  
Example of default (no-arg) constructor
Output:

Rule: If there is no constructor in a class, compiler automatically creates a default


constructor.

What is the purpose of a default constructor?


The default constructor is used to provide the default values to the object like 0, null, etc.,
depending on the type.

Example of default constructor that displays the default


values
public class Student3{
int id;
String name;

void display()
{
System.out.println(id+" "+name);
}

public static void main(String args[])


{
Student3 s1=new Student3();
Student3 s2=new Student3();

s1.display();
s2.display();
}

}
Output

0 null
0 null

Java Parameterized Constructor


A constructor which has a specific number of parameters is called a parameterized
constructor.

Why use the parameterized constructor?


The parameterized constructor is used to provide different values to distinct objects.
However, you can provide the same values also.

Example of parameterized constructor


In this example, we have created the constructor of Student class that have two
parameters. We can have any number of parameters in the constructor.

public class Student4{


int id;
String name;

Student4(int i, String n){


id = i;
name = n;
}

void display()
{
System.out.println(id+" "+name );
}

public static void main(String args[])


{
Student4 s1 = new Student4(111,"Kareem");
Student4 s2 = new Student4(222,"Abdullah");
s1.display();
s2.display();
}
}

Ouput
111 Kareem
222 Abdullah

Constructor Overloading in Java


In Java, a constructor is just like a method but without return type. It can also be
overloaded like Java methods.

Constructor overloading in Java is a technique of having more than one constructor with


different parameter lists. They are arranged in a way that each constructor performs a
different task. They are differentiated by the compiler by the number of parameters in the
list and their types.

Example of Constructor Overloading

public class Student5{


int id;
String name;
int age;
Student5(int i, String n){
id = i;
name = n;
}
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
}
void display(){System.out.println(id+" "+name+" "+age);}

public static void main(String args[]){


Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
}

Output

111 Karan 0
222 Aryan 25

You might also like