Constructor in java

Constructor in Java

Here, on this page, we will discuss constructor in java programming,

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. In Java, a constructor is a block of codes similar to the method.

Java Constructor

How Java Platform Independent

Java constructors or constructors in Java is a terminology been used to construct something in our programs. A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.

In Java, a constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called.

How Constructors are Different From Methods in Java?

  • Constructors must have the same name as the class within which it is defined while it is not necessary for the method in Java.
  • Constructors do not return any type while method(s) have the return type or void if does not return any value.
  • Constructors are called only once at the time of Object creation while method(s) can be called any number of times.

Now let us come up with the syntax for the constructor being invoked at the time of object or instance creation.

class Prepinsta
{   
  .......

  // A Constructor
  new Prepinsta() {}

  .......
}

// We can create an object of the above class
// using the below statement. This statement
// calls above constructor.
Geek obj = new Geek();


Types of Constructors in Java
Now is the correct time to discuss types of the constructor, so primarily there are two types of constructors in java:

  • No-argument constructor
  • Parameterized Constructor

1. No-argument constructor

A constructor that has no parameter is known as the default constructor. If we don’t define a constructor in a class, then the compiler creates a default constructor(with no arguments) for the class. And if we write a constructor with arguments or no-arguments then the compiler does not create a default constructor.

Run
import java.io.*;

class Prep{
	int num;
	String name;

	// this would be invoked while an object
	// of that class is created.
	Prep() { System.out.println("Constructor called"); }
}

class Main {
	public static void main(String[] args)
	{
		// this would invoke default constructor.
		Prep prep1 = new Prep();

		// Default constructor provides the default
		// values to the object like 0, null
		System.out.println(prep1.name);
		System.out.println(prep1.num);
	}
}
Constructor called
null
0

2. Parameterized Constructor

A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with our own values, then use a parameterized constructor.

Example:

Run

import java.io.*;

// Class 1
class Prep {
	// data members of the class.
	String name;
	int id;

	Prep(String name, int id)
	{
		this.name = name;
		this.id = id;
	}
}

class Main {
	// main driver method
	public static void main(String[] args)
	{
		// This would invoke the parameterized constructor.
		Prep prep1 = new Prep("adam", 1);
		System.out.println("PrepName :" + prep1.name
						+ " and PrepId :" + prep1.id);
	}
}
PrepName :adam and PrepId :1

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription