constructor in java

What is a constructor in java? | Java Advanced | Part – 3

A constructor is a function with same name as a class that is used to initialize class attributes etc. this function is automatically called when we create an object of that class.

A constructor is a function that has the same name as the class.

public class learningConstructor{

public learningConstructor(){

//Constructor

//statements

}

}

In the above snippet learningConstructor() is a constructor which has the same name as the class.

Constructors are often referred to as the special types of methods in Java.

Every class has a constructor. A constructor “constructs” its own class.

Constructors are not considered the members of the class.

If we do not write a constructor the java compiler writes one for us.

The constructor that java compiler writes for us is known as the Default constructor and it is inserted in the code during compilation.

But, if we write a constructor the default constructor is never created.

Every time an instance of the object is created the constructor is called.

A constructor is used to initilize class fields when an object of that class is created.

Just like any other method, a constructor may or may not have any parameters.

If a constructor has parameters it is known as a parameterized constructor.

Constructors in action

Let’s begin by writing a simple constructor.

public class learningConstructor{

public learningConstructor(){

System.out.println(“Hi! I am a constructor :D”);

}

}

We have written a simple constructor which prints “Hi! I am a constructor :D” every time a new instance of the object is created.

public class LearningConstructor{

public LearningConstructor(){

System.out.println(“Hi! I am a constructor :D”);

}

public static void main(String[] args) {

LearningConstructor learning1 = new LearningConstructor();

}

}

We have created a new instance of the object. The above program on execution gives the output.

Hi! I am a constructor 😀

new keyword is used while creating a new instance of the object.

We saw how a constructor without parameters works.

Let’s explore how we can initialize the class members using a constructor.

For this, we will use a Parameterized constructor.

public class LearningConstructor{

String name;

String type;

public LearningConstructor(String newName, String newType){

this.name = newName;

this.type = newType;

}

}

In the above snippet, we have a constructor  named LearningConstructor() which is having two parameters newName and newType both of type String.

See anything strange? this.name? this.type?

this is a keyword in Java.

It is used to reference the current instance of the method where it is used.

Thus

helloFunction(int a){

this.someVariable = a;

}

Here, this.someVariable refers to the instance of the method every time it is called.

Come back, come back. We were learning about parameterized constructors.

 public class LearningConstructor{

String name;

String type;

public LearningConstructor(String newName, String newType){

this.name = newName;

this.type = newType;

}

}

Let’s create an object of type LearningConstructor.

public class LearningConstructor{

String name;

String type;

public LearningConstructor(String newName, String newType){

this.name = newName;

this.type = newType;

}

public static void main(String[] args){

LearningConstructor learningConstructor1 = new LearningConstructor(“Hi”, “Default”);

}

}

Awesome, we have an object of type LearningConstructor. Let’s write a method which displays the values assigned to the members of the class.

public class LearningConstructor{

String name;

String type;

public LearningConstructor(String newName,String newType){

this.name = newName;

this.type = newType;

}

void display(){

System.out.println(“Name: “+name+” Type: “+type);

}

public static void main(String[] args){

LearningConstructor obj1 = new LearningConstructor(“Hi”, “Default”); obj1.display();

}

}

The output of the above mentioned code is

Name: Hi Type: Default

The main method created an instance of the class LearningConstructor, setting name to “Hi” and Type to “Default”. We then called the display() method of the obj1 object.

Wow! We have learned a lot about constructors.

Starting from what is constructor, default constructor, constructor without parameters till parameterized constructor.

To Summarize

  • Constructor is a special function with same name as class which is used to initialize class atributes etc. this function is automatically called when we create an object of that class.
  • If we do not write a constructor the java compiler writes one for us which is known as default constructor.
  • Every time an instance of the object is created the constructor is called.
  • A constructor is used to provide initial values to the class fields when an object is created.
  • If a constructor has parameters it is known as a parameterized constructor.

Recommended Book for constructor in java

Don't Miss

Leave a Comment

Your email address will not be published. Required fields are marked *

PYQ of History UGC NET UGC NET Mathematical Reasoning and Aptitude ICT (Digital Initiatives) | UGC NET | paper – 1 The Scope of Information and Communication Technology(ICT) PYQ of People, Development, and Environment Top 30 PYQ of HINDI | UGC NET – 2023 Top 30 PYQ of Teaching Aptitude PYQ of Research Aptitude | NTA UGC NET | Paper 1 | Part 1 UGC NET Paper 1 Syllabus | Updated | 2023 Types of Research | Research Aptitude | nta ugc net | UGC NET 2023