• Real-Time-Automation

Constructors in Java

Updated: Aug 2

A Java constructor is a special method that is called when an object is instantiated. In other words, when you use the new keyword. The purpose of a Java constructor is to initialize the newly created object before it is used.


Constructor: A special function which has the same name as of class name. Constructors don't have a return type and they don't return any value.


Usage of Default Constructor: It is mainly used for the initialization purpose. Whenever we define fields/properties in our class they generally have default values.


So, instead of keeping default or garbage values constructor gives us an opportunity to initialize the fields with our own set of values according to the requirement.

// Java Program to illustrate calling a 
// default or no-argument constructor 
import java.io.*; 
  
class RealTimeAutomation
{ 
 int num; 
 float val;
 String name; 

class Rta 
{ 
 public static void main (String[] args) 
 { 
 // this would invoke default constructor. 
 RealTimeAutomation real= new RealTimeAutomation(); 
  
 // Default constructor provides the default 
 // values to the object like 0,0.0, null 
 System.out.println(real.name); 
 System.out.println(real.num); 
 System.out.println(real.val);
 } 
} 

Output

null
0
0.0

Parameterized Constructor: A constructor that has parameters is known as parameterized constructor. If we want to initialize fields of the class with your own values, then use a parameterized constructor.

// Java Program to illustrate calling a 
// default or no-argument constructor 
import java.io.*; 
  
class RealTimeAutomation
{ 
 int num; 
 float val;
 String name; 
 
 // constructor would initialize data members 
 // with the values of passed arguments while 
 // object of that class created. 
 RealTimeAutomation(String name,float val, int id) 
 { 
 this.name = name; 
 this.val= val; 
 this.id = id; 
 }
 class Rta 
{ 
 public static void main (String[] args) 
 { 
 // this would invoke default constructor. 
 RealTimeAutomation real= new RealTimeAutomation("realtime",1.0,1); 
  
 // Default constructor provides the default 
 // values to the object like 0,0.0, null 
 System.out.println(real.name); 
 System.out.println(real.num); 
 System.out.println(real.val);
 } 
} 

Output

realtime
1
1.0

Constructor Overloading: to achieve constructor overloading we can have different constructors with different parameters. Compiler differentiates constructors on the basis of numbers of parameters, types of the parameters and order of the parameters.

// Java Program to illustrate calling a 
// default or no-argument constructor 
import java.io.*; 
  
class RealTimeAutomation
{ 
 int num; 
 String name; 
 
 // constructor with two arugments  
 RealTimeAutomation(String name, int id) 
 { 
   System.out.println(name+"  "+id); 
 }
 
  // constructor with one arugments
 RealTimeAutomation(int id) 
 { 
   System.out.println(id); 
 }
 class Rta 
{ 
 public static void main (String[] args) 
 { 
 // invoke constructor with two arguments
 RealTimeAutomation real= new RealTimeAutomation("realtime",1); 
 // invoke constructor with one arguments
 RealTimeAutomation real= new RealTimeAutomation(2); 
  
} 

Output

realtime  1
2

Points to remember

  1. A constructor is used to initialize the state of an object.

  2. A constructor must not have a return type.

  3. The constructor is invoked implicitly.

  4. The Java compiler provides a default constructor if you don't have any constructor in a class.

  5. The constructor name must be same as the class name.



266 views

©2020 by Real Time Selenium WebDriver Automation.