What is a Constructor in Java?

Last updated on Nov 27,2019 17.4K Views

What is a Constructor in Java?

edureka.co

Java is a versatile language when it comes to programming. Even though it is quite easy to learn, one must master the fundamental concepts first. One such concept is constructor in Java, it is a very important concept since it involves classes and objects. A constructor is a special method which is used to assign values to the objects. In this article, we will learn the following topics in detail:

What is a Constructor in Java?

We create a constructor to initialize an object. They have the same name as the class but have no explicit return type. It can be used to set initial values for object attributes. It is similar to a Java method

At the time of calling the constructor, the memory is allocated for the object. Each class in Java has a constructor. Even if you do not create one, Java implicitly calls a constructor with all data members value set to zero.


class Edureka
{
//constructor
new Edureka( )
}
//object is made and constructor is called.
Edureka ob1 = new Edureka( )

When is a Constructor Called?

A constructor is called when an object or an instance is created. It is used to assign values to the data members of the same class.

Rules For Constructors in Java

  1. The name of the constructor should be the same as that of the class name.
  2. A constructor cannot be declared as final, static, synchronized or abstract.
  3. It cannot have an explicit return type.
  4. A constructor can have an access modifier to control the access.

You should follow these rules while creating a constructor.

Types of Constructors in Java

There are two types of constructors

  1. Default Constructor
  2. Parametrized Constructor

Default Constructor

A constructor with no arguments is called a default constructor. If we do not create a constructor of a class, Java creates a default constructor with data members which has values like zero, null, etc.

But, if we specify a constructor with no arguments, it will be a default constructor or a no argument constructor which is another name for default constructor. Following is an example to show how to use a default constructor in Java:


class Edureka {
//creating the constructor
Edureka( )
{
System.out.println( 'hello learner') }

public static void main(String args[])
{
Edureka ob1 = new Edureka( )
}
}
output : hello learner

Parametrized Constructor

A constructor which has arguments is called as a parametrized constructor. It is used to assign values to distinct objects. Following is an example to show how we declare a parameterized constructor in java:

class Edureka {
string name, course;
//creating a parametrized constructor
Edureka(string s , string n )
{
  name = s ;
  course = n;
}
void show( )
{ System.out.println( name+ " " + course); }
 
public static void main(String args[])
{
 Edureka ob1 = new Edureka("Java" , "J2EE");
 Edureka ob2 = new Edureka('Java" , "Advance Java");
 ob1.show( );
 ob1.show( );
     }
}
output: Java J2EE
        Java Advance Java

Constructor Overloading

Just like method overloading, constructors can be overloaded to create objects in different ways. The compiler differentiates constructors based on how many arguments are present in the constructor and other parameters like the order in which the arguments are passed.

Following is an example of constructor overloading:

class Edureka 
{
 string name, course, technology;
 Edureka(string s , string n)
{
name = s ;
course = n ;
}
Edureka(string s , string n , string c)
{
name = s;
course = n;
technology = c;
}
void show( ) 
{ System.out.println(name +""+course+""+technology); }

public static void main(String args[ ])
{
 Edureka ob1 = new Edureka("edureka" , "Java") ;
 Edureka ob2 = new Edureka("edureka" , "J2EE" , "Java");
 ob1.show( );
 ob2.show( );
    }
}
output: edureka Java
        edureka J2EE Java

Difference Between Method And Constructor

MethodConstructor
  • Method name need not be same as the class name
  • Constructor name has to be same as the class name
  • Method has a return type
  • Constructor does not have a return type
  • You can call a method any number of times
  • Constructor is called when an object is created

In this blog, we have discussed constructors in java, how we use them and different types of constructors as well. Java is an interesting language, but it becomes tricky if the fundamentals are not clear. To kick-start your learning and master all the skills related to java technology enroll to the java certification program and unleash the java developer in you.

Got a question for us? please mention this in the comments section of this ‘What is a Java Constructor?’ article and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES