WHAT IS CONSTRUCTOR IN JAVA

  • We create a constructor to initialize an object. They have the same name as the class but have no explicit return type.
  • It can be used to set initial values for object attributes. It is similar to a Java method
  • At the time of calling the constructor, the memory is allocated for the object. Each class in Java has a constructor.
  • Even if you do not create one, Java implicitly calls a constructor with all data members value set to zero.

Rules For Constructors in Java:

  • The name of the constructor should be the same as that of the class name.
  • It cannot have an explicit return type.
  • A constructor can have an access modifier to control the access.
  • A constructor cannot be declared as final, static, synchronized or abstract.

Default Constructor:

  • A constructor with no arguments is called a default constructor. 
  • If we do not create a constructor of a class, Java creates a default constructor with data members which has values like zero, null, etc.
  • But, if we specify a constructor with no arguments, it will be a default constructor or a no argument constructor which is another name for default constructor.

NOTE: 1. Default Constructor is invisible.

2. If there is no Constructor in a class, Compiler Automatically Creates a Default Constructor.

Constructor Overloading:

  • Java Constructor overloading is a technique in which a class can have any number of constructors that differ in parameter list.
  • The compiler differentiates these constructors by taking into account the number of parameters in the list and their type.
  • Overloaded constructor is called based upon the parameters specified when new is executed.

Difference Between Method And Constructor:

Method Constructor
Method name need not be same as the class nameConstructor name has to be same as the class name
Method has a return typeConstructor does not have a return type
You can call a method any number of timesConstructor is called when an object is created

What is this keyword in Java?

  • this keyword in java represents the current instance of a class. It is mainly used to access other members of the same class.
  • With the help of this keyword, you can access methods, fields, and Constructors of the same class within the class.

What is this keyword in Java?

The main purpose of using this keyword is to differentiate the formal parameter and data members of class.

 If in case, the formal parameter and data members of the class are the same, then it leads to ambiguity.

this . (this dot)

which can be used to differentiate variable of class and formal parameters of method or constructor.

Sample Program:

package chennai.velachery;

public class Bank2
{
String name,emailid,alt;
int mobileno,aadhar,altmobile;

public Bank2(String name,String emailid,int mobileno,int aadhar)
{
this.name=name;
this.emailid=emailid;
this.mobileno=mobileno;
this.aadhar=aadhar;

}
public Bank2(String name,String emailid,int mobileno,int aadhar,int altmobile)
{
this.name=name;
this.emailid=emailid;
this.mobileno=mobileno;
this.aadhar=aadhar;
this.altmobile=altmobile;

}
public static void main(String[]args)
{
Bank2 cust1 = new Bank2(“Raja”,”raj@raj.com”,1234,2345);
//cust1.name=”Raja”;
//cust1.emailid=”raj@raj.com”;
//cust1.mobileno=1234;
//cust1.aadhar=2345;
cust1.openAccount();
cust1.getloan();

Bank2 cust2 = new Bank2(“Kumar”,”kumar@raj.com”,9786,2655,4590);
//cust2.name=”Kumar”;
//cust2.emailid=”kumar@raj.com”;
//cust2.mobileno=9786;
//cust2.aadhar=2655;
//cust2.alt =”kumaran@kumar.com”;
//cust2.altmobile=4590;
//cust2.openAccount(name,emailid,mobileno,aadhar,alt,altmobile);
cust2.openAccount();

Bank2 cust3=new Bank2(“Babu”,”babu@ram.com”,3456,9976);
cust3.openAccount();

}
public void getloan()
{
System.out.println(name);
System.out.println(emailid);
System.out.println(mobileno);

}
public void openAccount()
{
System.out.println(“Open Account”);
System.out.println(“Customer Submitted”);
System.out.println(name);
System.out.println(emailid);
System.out.println(mobileno);
System.out.println(aadhar);
//System.out.println(alt);
System.out.println(altmobile);
}
}

OUTPUT:

Leave a comment

Design a site like this with WordPress.com
Get started