A Brief View of Java Constructors
Tags: java, advancedjava, constructors, j2ee
Published: 2019-01-08 06:28:49

---
title: A Brief View of Java Constructors
published: True
description: 
tags: Java, Advanced Java, Constructors, J2ee
---

Introduction

Java is one of the emerging and on-demand technologies in the current scenario. Being the most secure and reliable and having huge features, it was become one among the on-demand ones.

We all know that the objects play a crucial role in Java and are created using initial values provided in two ways. It is a tedious approach initializing variables of the object in this manner.One way is using dot operator to access the instance variable and them assigning values individually. The other way is using Constructors- the easiest and the most preferred way to assign values to variables in Java.

Now, let’s discuss about the Constructors and their importance in assigning values to variables in Java.

Constructor

A constructor is a special process of the  structure or class into <a href="https://tekslate.com/advanced-java-training">object-oriented programming</a> that initializes to us an object of that type. and can be used to define the values of the members of an object, either user-defined values or to default. A constructor is an instance methods and variables it usually has the same name as of class, 
1. Rules of the constructor:

a. Constructor name should be same as a Class name.
b. Constructor can’t have any return type even Void.

2. Constructor is executed during the object creation.
3. A constructor can have an n number of arguments.
4. The variable which is declared inside the constructor is called local variables.
5. Constructor can be categorised into 2 types:

    a. Default Constructor.
    b. User define constructor.

User define constructor: 

The constructor which is generated by user is called user defined
constructor. It is also called as an explicit constructor.
It can be categorised into:

i) User define parameterised Constructor: If the <a href="https://tekslate.com/reactjs-training/">constructor</a> is containing any arguments or if the
user is passing any value inside the constructor is called user define parameterised constructor.

Ex:

public class Demo {
Demo(String s, int i) // Parameterized constructor
{
System.out.println(&quot;value of I is &quot;+i+ &quot;\nvalue of S is &quot;+s);

}
void display()// Method
{
System.out.println(&quot;I am inside the Display method&quot;);
}
public static void main(String[] args)
{
Demo d = new Demo(&quot;Rashda&quot;,10);
d.display();
}
}

Run as class your programme until your Output window displays the following:

Output: 

value of I is 10
Value of S is Rashda
I am inside the Display method


ii) User define Non parameterised Constructor: If the constructor is not having any argument or if
the user is not passing any value inside the constructor is called user define non parameterised
constructor.

Ex:

public class Demo {
Demo() // Non parameterized constructor
{
System.out.println(&quot;I am inside the constructor&quot;);
}
void display()// Method
{
System.out.println(&quot;I am inside the Display method&quot;);
}
public static void main(String[] args) {
Demo d = new Demo();
d.display();
}
}

The programme then moves non parameterized  constructor on to the next line of code

Output:

I am inside the constructor
I am inside the Display method

Default Constructor:

1. The constructor which is generated by compiler is called default Constructor.
2. The Default Constructor does not have any implementation (statement).
3. Every CLASS has a default constructor.
4. Default constructor is also called as Implicit Constructor.

Ex:

1. public class Demo {

public static void main (String[] args) {
Demo d = new Demo(10);
}
}
output: gives compile time error , because there is no parameterized constructor, which is present
inside the class.
2. public class Sample{
public static void main(String[] args) {
Sample s = new Sample();
}
}

Now it becomes to the gives compile time error, because there is no parameterized constructor



Output: 

No compile time error
In the above program JVM executed the default constructor which is generated by compiler.

Note:
1. If a user is defining any constructor inside the class, then compiler will not generate any default
constructor.
2. If the user is not defining any constructor inside the class then only compiler will generate default
constructor.
3. Constructor constructs an object and initialises the state.
Ex: public class Sample{
Sample(int i)
{
system.out.println(i);
}
public static void main(String[] args) {
Sample s = new Sample(10);
Sample s1 = new Sample();
}
}

Output: 
gives compile time error, because there is no default constructor is present in the above
class.

Purpose of Constructor:

1. Object creation.
2. To initialise the states i.e initialising the local variable values to the instance variables.

Note:

1. We can’t access the local variable value outside the method and outside the constructor.
2. Local variable value is within the method scope or within the constructor scope.
3. Constructor can’t return any value.



Ex:

class Student
{
String name;

int id;
String collegename;
Student(String n,int i,String c) // Constructor
{
name = n;
id = i;
collegename = c;
}
void display()
{System.out.println(&quot;Name is &quot;+name+&quot;\n Id is &quot;+ id + &quot;\n college name is &quot;+ collegename);
}
}
public class Sample {
public static void main(String[] args) {
Student S1 = new Student (&quot;John&quot;,1,&quot;KIT&quot;);
S1.display();
Student S2 = new Student (&quot;Mike&quot;,2,&quot;KIT&quot;);
S2.display();
Student S3 = new Student (&quot;Adam&quot;,3,&quot;KIT&quot;);
S3.display();
}
}

Input
Print " Names"  "ID's"  "College Names"

Output:

Name is John
Id is 1
college name is KIT
Name is Mike
Id is 2
college name is KIT
Name is Adam
Id is 3
college name is KIT

Note:

Whenever the local variable and instance variable name are same JVM will get
confused to identify local variable and instance variable. If we want to overcome this
problem we have to use this keyword. this is a keyword it always refers to the instance
variables of the current class.

Ex:

1. class Student
{
String name;

int id;
String collegename;
Student(String name, int id, String collegename) // Constructor
{
name = name;
id = id;
collegename = collegename;
}
void display()
{
System.out.println(&quot;Name is &quot;+name+&quot;\n Id is &quot;+ id + &quot;\n college name is &quot;+ collegename);
}
}
public class Sample {
public static void main(String[] args) {
Student S1 = new Student (&quot;John&quot;,1,&quot;KIT&quot;);
S1.display();
Student S2 = new Student (&quot;Mike&quot;,2,&quot;KIT&quot;);
S2.display();
Student S3 = new Student (&quot;Adam&quot;,3,&quot;KIT&quot;);
S3.display();
}
}




Input:
Print “name = name;
       id = id;
      collegename = collegename;”

output:
Name is Null
Id is 0
college name is Null
Name is Null
Id is 0
college name is Null
Name is Null
Id is 0
college name is Null

Ex2:
class Student
{
String name;
int id;
String collegename;
Student(String name,int id,String collegename) // Constructor
{

this.name = name;
this.id = id;
this.collegename = collegename;
}
void display()
{
System.out.println(&quot;Name is &quot;+name+&quot;\n Id is &quot;+ id + &quot;\n college name is &quot;+ collegename);
}
}
public class Sample {
public static void main(String[] args) {
Student S1 = new Student (&quot;John&quot;,1,&quot;KIT&quot;);
S1.display();
Student S2 = new Student (&quot;Mike&quot;,2,&quot;KIT&quot;);
S2.display();
Student S3 = new Student (&quot;Adam&quot;,3,&quot;KIT&quot;);
S3.display();
}
}

Input:
String name;
int id;
String collegename;

Print "this.name = name;
this.id = id;
this.collegename = collegename;"




Output:

Name is John
Id is 1
college name is KIT
Name is Mike
Id is 2
college name is KIT
Name is Adam
Id is 3
college name is KIT

# Default Value:

String = Null
int = 0
byte = 0
short = 0
long = 0
float = 0.0
double = 0.0
char = space
Boolean = false

Conclusion:

Thus, constructors add a lot on usability and readability of any class in Java. It would be difficult to test classes since object creation without the knowledge about the constructor and it’s working.  

In this article, we’ve learned about how constructors work in Java and why they’re essential. In fact, using constructor has become easier as you can use dependency injection for class initialization. While working with open sources like Google Guice and Spring frameworks, it will become easy to use and test these classes with appropriate constructors.