---
title: Constructor in Java - Scaler Topics
description: Learn about Constructor in Java by Scaler Topics. Java Constructors give some value to the state of an object.
author: Kishore Choudhury
category: Java
---
:::section{.main}
In Java, Constructors initialize the state of an object during the time of object creation. The constructor in java is called when an object of a class is created. Constructors must have the same name as the class within which it is defined.
:::
:::section{.main}
## What is Constructor in Java?
A **constructor in Java**, in the simplest terms, is a specialized method responsible for initializing an object. It is called at the time of object creation and can be used to set initial values of an object's data members.
**Syntax:**
Syntax is pretty straightforward. It is the same as how one would define a class but without the class keyword.
This is the syntax for the class:
```java
public class className{
// Class definition
}
```
This is the syntax for its constructor:
```java
public className(parameterList) {
// parameterList is a list of values and their types for example:
// type1 data1, type2 data2, ....
// Assign the input parameters to class members
// Perform any other task required
}
```
Classes are blueprints of objects specifying an object's various fields and methods. However, constructors in Java are specialized methods that can be used to assign values to those fields.
Let’s look at the **code snippet** below:
```java
class Dogs {
private String name;
private String breed;
public Dogs() {
// No initialization parameters
}
public Dogs(String breed) {
this.breed = breed;
}
public Dogs(String name, String breed) {
this.name = name;
this.breed = breed;
}
void isRunning(int runStatus) {
if (runStatus == 1) {
System.out.println("Dog is running");
} else {
System.out.println("Dog is not running");
}
}
void tailWagging(String tailStatus) {
if (tailStatus.equals("Woof")) {
System.out.println("Dog is wagging its tail");
} else {
System.out.println("Dog is not wagging its tail");
}
}
}
public class Main {
public static void main(String[] args) {
Dogs dog1 = new Dogs();
Dogs dog2 = new Dogs("Bulldog");
dog1.isRunning(0);
dog2.tailWagging("Woof");
// A dog with both name and breed initialized at the time of object creation.
Dogs dog3 = new Dogs("Snoopy", "German Shepherd");
}
}
```
**Output:**
```plaintext
Dog is not running
Dog is wagging its tail
```
**Explanation:**
*`“new”` is a java keyword that is used to create an instance of the class.* `dog3` of type Dogs equals a ‘new’ Dogs with `name = "Snoopy" and breed = "German Shepherd"`. The dog `dog3` is initialized with these parameters.
`new Dogs(name, breed)` is a call to the constructor. The parameters `name` and `breed` represent the initial state of the new object being created.
:::
:::section{.main}
## Rules for Creating Java Constructor
There are some important and basic rules to create a constructor in Java.
* Constructor names are always the same as the class name.
* Constructors never have a return type.
* There are four keywords in Java that is **NEVER** associated with a constructor – **abstract**, **final**, **static**, and **synchronized**.
* Constructors in Java are, therefore, also called special methods, which are invoked automatically at the time of object creation.
:::
:::section{.main}
## Types of Constructor in Java
### Default Constructor (No-Arg Constructors)
As the name suggests, these constructors do not have any arguments since they are created by default in Java when the programmer writes no constructors.
Consider the example of the Dogs class.
```java
class Dogs {
private String name;
private String breed;
void isRunning(int runStatus) {
if (runStatus == 1) {
System.out.println("Dog is running");
} else {
System.out.println("Dog is not running");
}
}
void tailWagging(String tailStatus) {
if (tailStatus.equals("Woof")) {
System.out.println("Dog is wagging its tail");
} else {
System.out.println("Dog is not wagging its tail");
}
}
}
public class Main {
public static void main(String[] args) {
Dogs dog1 = new Dogs();
Dogs dog2 = new Dogs();
dog1.isRunning(2);
dog2.tailWagging("Woof");
}
}
```
**Output:**
```plaintext
Dog is not running
Dog is wagging its tail
```
**Explanation:**
In the above code snippet, there are **no constructors** defined. In such a case, Java would automatically create a constructor without parameters. Hence, it is also called the [default constructor](https://www.scaler.com/topics/default-constructor-in-java/). It does not initialize any member of the class. It simply creates the object with no initial state.
### Parameterized Constructor
In this case, the programmer writes the constructors with one or more arguments. This allows distinct or same values to be assigned to the object’s data member during object creation.
```java
public class Dogs {
private String name;
private String breed;
public Dogs(String name, String breed) {
this.name = name;
this.breed = breed;
}
}
```
:::
:::section{.main}
## Constructor Overloading in Java
In Java, constructors are similar to methods but without a return type. They can be overloaded, just like methods, allowing for the creation of multiple constructors with different parameter lists. Each constructor can serve a distinct purpose, with the compiler distinguishing them based on the number and types of parameters they accept. This technique, known as constructor overloading, enables flexibility in initializing objects based on varying sets of input parameters.
```java
class Dogs {
private String name;
private String breed;
public Dogs() {
// No initialization parameters
}
public Dogs(String breed) {
this.breed = breed;
}
public Dogs(String name, String breed) {
this.name = name;
this.breed = breed;
}
void isRunning(int runStatus) {
if (runStatus == 1) {
System.out.println("Dog is running");
} else {
System.out.println("Dog is not running");
}
}
}
public class Main {
public static void main(String[] args) {
Dogs dog1 = new Dogs();
Dogs dog2 = new Dogs("Bulldog");
dog1.isRunning(0);
dog2.tailWagging("Woof");
// A dog with both name and breed initialized at the time of object creation.
Dogs dog3 = new Dogs("Snoopy", "German Shepherd");
}
}
```
**Output:**
```plaintext
Dog is not running
Dog is wagging its tail
```
Now, if one wanted to create an object of type Dogs with the same name or say with name and breed, they would be able to.
They are different in the number of parameters, and they are the same in that all of them allow for object creation, although with some differences.
:::
:::section{.main}
## Difference between Constructors and Methods in Java
The table below has the differences between constructors and methods listed out:
| CONSTRUCTORS |METHODS |
| :--------: | :--------: |
|Constructors always have the same name as the class for which they are being created.| Method names could be anything but the class name. |
| Constructors never have any return type. | Methods can return any value or nothing by using the keyword `void`. |
| The usage of a constructor in Java is minimal compared to a method. It is called only when the object of a class is created, while methods can be called whenever. Constructors are invoked implicitly. | Methods can be called whenever the programmer desires them. Methods are called explicitly. |
| Functionality-wise, constructors are used to initialize an object | Methods are used to execute a specific operation. |
| A constructor would be created by default if the user provides none | Methods must be created.
:::
:::section{.main}
## Copy Constructor in Java
A [Copy Constructor](https://www.scaler.com/topics/copy-constructor-in-java/) in Java is used to create an object with the help of another object of the same Java class.
Let’s say we create an object using the copy constructor. The copy constructor has at least one object of the same class as an argument. The primary objective of the copy constructor is to create a new object with the same properties as that of the passed argument.
Look at the code snippet below:
```java
class Dogs {
private String name;
private String breed;
private int taillength;
public Dogs(String name, String breed) {
this.name = name;
this.breed = breed;
}
public Dogs(Dogs dog) {
this.name = dog.name;
this.breed = dog.breed;
this.taillength = dog.taillength;
}
public Dogs(String name, String breed, int taillength) {
this.name = name;
this.breed = breed;
this.taillength = taillength;
}
void isRunning(int val1) {
if (val1 == -1) System.out.println(
"Dog is running"
); else System.out.println("Dog is not running");
}
void tailWagging(String val2) {
if (val2.equals("Woof")) System.out.println(
"Dog is wagging its tail"
); else System.out.println("Dog is not wagging its tail");
}
}
public class Main {
public static void main(String[] args) {
Dogs dog1 = new Dogs("Tommy","Golden Retriever",196);
Dogs dog2 = new Dogs(dog1);
}
}
```
`dog2` is created using the copy constructor where `dog1` is passed as an argument. The data members of `dog2` are initialized using the data members of the `dog1`.
:::
:::section{.main}
## How to Copy Values without Constructor?
We can achieve object value copying without using a constructor in java by directly assigning the values of one object to another.
In this example, we have a class called `Dog` with two fields: `age` and `name`. We've defined a constructor to initialize these fields, along with a default constructor and a method display() to print the values of the fields.
```java
class Dog {
int age;
String name;
// Parameterized constructor
Dog(int years, String dogName) {
age = years;
name = dogName;
}
// Default constructor
Dog() {}
// Method to display dog details
void display() {
System.out.println(name + " is " + age + " years old.");
}
public static void main(String args[]) {
// Creating instances of Dog
Dog dog1 = new Dog(5, "Buddy");
Dog dog2 = new Dog();
// Copying values from dog1 to dog2
dog2.age = dog1.age;
dog2.name = dog1.name;
// Displaying details of both dogs
dog1.display();
dog2.display();
}
}
```
**Output**
```plaintext
Buddy is 5 year old
Buddy is 5 year old
```
:::
:::section{.summary}
### Conclusion
* Constructor in Java is used to initialise the data members of an object at the time of object creation.
* There are two major types of constructors in java: **Parameterized** and **No-Arg** constructors.
* The No-Arg constructors in Java are created by default in Java and take no arguments.
* The Parameterized constructer can be used to pass values, allowing values to be assigned to the state of an object during creation.
* The copy constructor in Java is a constructor that creates an object by initializing it with an object of the same class that has been created previously.
:::
:::section{.faq-section}
## FAQs
Q: **What is this () in Java constructor?**
A: In Java, the parentheses () in a constructor declaration represent the parameter list the constructor may accept.
Q: **What is the difference between a constructor and a destructor?**
A: A constructor is called when an object is created, while when an object is destroyed or goes out of scope, then the destructor is called.
:::