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 object-oriented programming 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,
- Rules of the constructor:
a. Constructor name should be same as a Class name.
b. Constructor can’t have any return type even Void.
- Constructor is executed during the object creation.
- A constructor can have an n number of arguments.
- The variable which is declared inside the constructor is called local variables.
-
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 constructor 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("value of I is "+i+ "\nvalue of S is "+s);
}
void display()// Method
{
System.out.println("I am inside the Display method");
}
public static void main(String[] args)
{
Demo d = new Demo("Rashda",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("I am inside the constructor");
}
void display()// Method
{
System.out.println("I am inside the Display method");
}
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:
- The constructor which is generated by compiler is called default Constructor.
- The Default Constructor does not have any implementation (statement).
- Every CLASS has a default constructor.
- Default constructor is also called as Implicit Constructor.
Ex:
- 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.
- 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:
- If a user is defining any constructor inside the class, then compiler will not generate any default constructor.
- If the user is not defining any constructor inside the class then only compiler will generate default constructor.
- 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:
- Object creation.
- To initialise the states i.e initialising the local variable values to the instance variables.
Note:
- We can’t access the local variable value outside the method and outside the constructor.
- Local variable value is within the method scope or within the constructor scope.
- 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("Name is "+name+"\n Id is "+ id + "\n college name is "+ collegename);
}
}
public class Sample {
public static void main(String[] args) {
Student S1 = new Student ("John",1,"KIT");
S1.display();
Student S2 = new Student ("Mike",2,"KIT");
S2.display();
Student S3 = new Student ("Adam",3,"KIT");
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:
- 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("Name is "+name+"\n Id is "+ id + "\n college name is "+ collegename);
}
}
public class Sample {
public static void main(String[] args) {
Student S1 = new Student ("John",1,"KIT");
S1.display();
Student S2 = new Student ("Mike",2,"KIT");
S2.display();
Student S3 = new Student ("Adam",3,"KIT");
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("Name is "+name+"\n Id is "+ id + "\n college name is "+ collegename);
}
}
public class Sample {
public static void main(String[] args) {
Student S1 = new Student ("John",1,"KIT");
S1.display();
Student S2 = new Student ("Mike",2,"KIT");
S2.display();
Student S3 = new Student ("Adam",3,"KIT");
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.
Top comments (0)