---
title: "Constructor in Java"  
description: "Java constructors are the methods which are used to initialize objects. Constructor method has the same name as that of class, they are called or invo"  
author: "Manoj Pandey"  
published: 2015-04-25  
updated: 2018-02-23  
canonical: https://www.mindstick.com/blog/10873/constructor-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Constructor in Java

Java constructors are the [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) which are used to initialize [objects](https://www.mindstick.com/forum/145447/what-are-objects). Constructor [method](https://www.mindstick.com/forum/166/webservice-method) has the same name as that of class, they are called or invoked when an object of the class is created and can't be called explicitly.\

A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—[except](https://www.mindstick.com/blog/144/union-intersection-and-except-operator-in-sql-server) that they use the name of the class and have no return type.

For example

```
class Sample{    Sample( )    { }}
```

\

##### Rules for creating java constructor

There are basically two [rules](https://www.mindstick.com/articles/43901/blackjack-rules) defined for the constructor.

- The constructor name must be same as its [class name](https://www.mindstick.com/forum/12871/how-to-get-class-name).
- The constructor must have no [explicit](https://www.mindstick.com/interview/2677/can-you-explain-about-implicit-and-explicit-type-casting) return type.

##### \

##### Types of Java constructors

There are two types of constructors:

- [Default constructor](https://www.mindstick.com/forum/34531/default-constructor) (no-arg constructor)
- Parameterized constructor

![Constructor in Java](https://www.mindstick.com/blogs/8064aa6c-b5cd-49b5-8ccc-774feda6299b/images/719df578-2a18-47f0-bf8d-7b49c32fb0ea.png)

**1. Default Constructor-:** A constructor that has no parameter is known as default constructor.

Example of the default constructor

```
class MyClas {      public MyClas() {           System.out.print("Default Constructor...");     }} class Sample {     public static void main(String[] args) {           MyClas clas=new MyClas();     }}
```

**2.** **Java parameterized constructor-:** A constructor that have parameters is known as a parameterized constructor.

Example of parameterized constructor

```
class Student{      int id;      String name;            Student(int i,String n){        id = i;        name = n;      }      void display(){System.out.println(id+" "+name);}         public static void main(String args[]){        Student s1 = new Student(111,"Zack");        Student s2 = new Student(222,"James Anderson");        s1.display();        s2.display();     }  }
```

Output-:\

111 Zack

222 James Anderson

\

**Notes-: Constructor return current class instance but we cannot use return type.**

\

\

Here I am creating a sample of constructor overloading

```
class Cricketer { String name; String team; int age; Cricketer ()   //default constructor. {  name ="";  team ="";  age = 0; } Cricketer(String n, String t, int a)   //constructor overloaded {  name = n;  team = t;  age = a; }Cricketer (Cricketer ckt)//constructor similar to copy constructor of c++  {  name = ckt.name;  team = ckt.team;  age = ckt.age; } public String toString()  {  return "this is " + name + " of "+team; }} Class test:{ public static void main (String[] args) {  Cricketer c1 = new Cricketer();  Cricketer c2 = new Cricketer("sachin", "India", 32);  Cricketer c3 = new Cricketer(c2 );  System.out.println(c2);  System.out.println(c3);  c1.name = "Virat";  c1.team= "India";  c1.age = 32;  System .out. print in (c1); }}
```

**Output-:**\

this is sachin of india

this is sachin of india

this is virat of india

---

Original Source: https://www.mindstick.com/blog/10873/constructor-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
