The constructor is a block or code which similar to a method that is called when the instance of an object is created. It is useful for memory management. It provides data for the objects. In other words, it constructs values, which is why it is known as a constructor.
Example 1:
package com.javabykiran.Constructor;
class Student {
int sid;
String sname;
String email;
long phone;
void Student() { //return type void and constructor does not have any return type
// It considered as a method as it has return type
System.out.println("method-student()");
}
Student() {
System.out.println("zero parameter const");
}
Student(int id, String sn, String em){
System.out.println("3 parameter const");
sid = id;
sname = sn;
email = em;
}
Student(int id, String sn, String em, long ph) {
System.out.println("4 parameter const");
sid = id;
sname = sn;
email = em;
phone = ph;
}
String show() {
System.out.println(sid);
System.out.println(sname);
System.out.println(email);
System.out.println(phone);
return"JBK";
}
}
package com.javabykiran.Constructor;
public class Lab5 {
public static void main(String[] args){
Student s1=new Student();
s1.show();
Student s2 = new Student(102, "kd,kiran@jbk.com",s1.show());
Student s3 = new Student(103, "kiran,Kiran@jbk.com",s2.show(),8888809416L);
s3.show();
s1.Student(); //calling the normal method,not the constructor
}
Output:
zero parameter const
0
null
null
0
0
null
null
0
3 parameter const
102
kd,kiran@jbk.com
JBK
0
4 parameter const
103
kiran,Kiran@jbk.com
JBK
8888809416
method-student()
Constructor will be used to initialize instance variable of the class with a different set of values, but it's not necessary to initialise it.
When you are not writing any constructor inside the class then the default constructor will inserted by the JVM automatically,[at compile time].[If the class is public then the constructor will automatically be public, if it is default, then it will have default access specifier.]
When you write any constructor, then default constructor will not be inserted by the JVM.
You cannot specify the return type for the constructor, but if you do specify, then it will be treated as a normal method.
You can call the constructor with following ways:
Constructor can be overloaded i.e. you can write other constructors by changing the arguments.
You cannot write two constructors with the same number of parameters and same kind of types.
Constructors cannot be overridden.
Any access specifiers can be applied to constructors, if private is applied to constructor, then we cannot create an object outside of class.
If somebody says, if they want to create class, no one should instantiate it. Then you can say that the constructor must be private.
Mostly private constructors are used in singleton design pattern.
What is the use of constructors? You can say that if some code needs to be executed at object creation, then we can write that code in constructor. Generally. It's for initialization of global variable.
Let’s say there is a use case, client want to send sms and email so we can design class as below (without constructor).
package com.javabykiran.Constructor;
class Greeting_Message {
String greeting_msg = "Hello";
String user_Name ="Guest";
String getEmailTxt(String emailMsg) {
String completeMsg = greeting_msg +" " + user_Name +
" " + emailMsg;
System.out.println(completeMsg);
return completeMsg;
}
String getSmsTxt(String smsMsg) {
String completeMsg = greeting_msg + " " + user_Name +
" " +smsMsg;
return completeMsg;
}
}
Now we can observe here client need to send greeting message every time in each method
So we need to reduce efforts of a client by using constructor.
See the below industrial example:
Example 2: Industrial Example
package com.javabykiran.Constructor;
class Greeting_Message {
String greeting_msg = "Hello";
String user_Name ="Guest";
Greeting_Message() {
// this constructor is just for default clients
}
Greeting_Message(String gMsg, String uName) {
greeting_msg = gMsg;
user_Name = uName;
}
String getEmailTxt(String emailMsg) {
String completeMsg = greeting_msg +" " + user_Name + " " + emailMsg;
System.out.println(completeMsg);
return completeMsg;
}
String getSmsTxt(String smsMsg) {
String completeMsg = greeting_msg + " " + user_Name + " " +smsMsg;
return completeMsg;
}
}
package com.javabykiran.Constructor;
public class Greeting_Message_Test{
public static void main(String[] args){
String emailMsg = "EMAIL :This is test email by javabykiran";
String smsMsg = "SMS: This is the best book for java learners";
System.out.println("****** ICICI*******");
// wants all default data
Greeting_Message greeting_Message = new Greeting_Message();
greeting_Message.getEmailTxt(emailMsg);
greeting_Message.getSmsTxt(smsMsg);
System.out.println("****** BOA*******");
// Different users and messages
Greeting_Message greeting_MessageBOA = new Greeting_Message ("Hello", "KiranBOA");
greeting_MessageBOA.getEmailTxt(emailMsg);
greeting_MessageBOA.getSmsTxt(smsMsg);
System.out.println("****** HDFC*******");
Greeting_Message greeting_MessageHDFC =new Greeting_Message ("Hi","KiranHDFC");
greeting_MessageHDFC.getEmailTxt(emailMsg);
greeting_MessageHDFC.getSmsTxt(smsMsg);
}
}
Output:
****** ICICI *******
Hello Guest EMAIL : This is test email by javabykiran
****** BOA *******
Hello KiranBOA EMAIL : This is test email by javabykiran
****** HDFC *******
Hi KiranHDFC EMAIL : This is test email by javabykiran