Constructors and Default Constructor

Constructors and Default Constructor

In Java programming, constructors are special calling methods in user-defined class programs. Class constructors are used to initialize object instance data for the class. When an object of a class is created in a Java program, they are first invoked on the class data type, and these class constructors are responsible for setting initial values ​​for the object’s instance variables.

Constructors and Default Constructor

What is a Class Constructor in Java?

A constructor is a special calling method in a Java class program. The class constructor name declared in the program must be the same as the class name. When an object or instance of a user-defined class is created, it calls itself within the class. The purpose of declaring a constructor in a Java class is to initialize the class object’s default state by assigning values ​​to its instance variables.

Elements of Class Constructor in Java.

  • Constructor Name – Remember, the name of a declared constructor in a Java class must be the same as the name of the actual class.
  • No Return Type – A declared constructor in a Java class has no return data type, not even a void function.
  • Automatic Invocation – When a new object is created in a class using the new keyword, the declared constructor in the class program is automatically called.

Types of Constructors in Java Classes

There are two main types of constructors in Java programming.

  • Default Constructor – A default constructor in Java programming is a constructor without parameters that helps initialize objects in a class program with default values.
  • Parameterized Constructor – A parameterized constructor in a Java class program is a constructor that takes input parameter values ​​from the Java user to initialize objects in a class program with a specific value.

Default Constructor in Java.

In Java programming, a default constructor is a user-defined constructor type that does not accept any arguments from the Java programmer or user and provides a default value to the class object. If Java programmers do not explicitly define any constructor methods in a class, the Java compiler provides the user with a no-argument constructor that initializes class instance variables to default values, such as null, 0, false, etc., depending on the data type.

If no constructor is provided in a Java class program, Java programming automatically provides a default constructor to the user, which initializes the instance variables declared in the class object to the default constructor value.

Java users can create or define their own default constructor in a class, or if you don’t have one defined in your existing class, Java classes provide one by default.

Syntax of a default constructor in Java.

public ClassName() {

// User-defined class constructor code

}

Example of a default constructor in Java programming.

public class Employee {

// Here we define instance employee class variables

String emp_name;

String age;

int salary;

// Here we declare the default constructor with no parameters value

public Employee() {

// Here we initialize employee class instance variables with default values

emp_name = “notknown”;

age = “notknown”;

salary = 0;

}

// here we use Method to display employee class information

public void empInfo() {

System.out.println(“employee emp_name is – ” + emp_name);

System.out.println(“employee age is – ” + age);

System.out.println(“employee salary is – ” + salary);

}

// here we define Main method

public static void main(String[] args) {

// here we are creating an object using the default constructor method

Employee newEmployee = new Employee(); // here The default constructor is called here

// here it Calling the method to display the details of newEmployee class element

newEmployee.empInfo();

}

}

Explaining default constructor in Java.

  • Here, a default constructor Employee() is defined in the Employee class, which initializes the instance variables (emp_name, age, salary) to default values ​​in the Employee class. When a newEmployee object is created using the newEmployee method, the class automatically calls the default constructor, which sets the values ​​of emp_name, age, and salary to “notknown”, “notknown”, and 00 in the specified order.
  • The empInfo() method is called to represent the default constructor values ​​in the Employee class.

Parameterized Constructor in Java.

In Java class programming, a parameterized constructor is a user-defined constructor method that accepts argument values ​​from the Java user in the class program. This allows the Java programmer to easily provide custom values ​​to initialize an object instance in the class program.

Syntax of a parameterized constructor in Java.

public ClassName(dataType parameter1, dataType parameter2, …) {

// User-defined class constructor code

}

Example of a parameterized constructor in a Java class.

public class Employee {

// Here we create an instance of the employee class variables

String emp_name;

String age;

int salary;

// Here we define a parameterized constructor with arguments

public Employee(String emp_name, String age, int salary) {

this.emp_name = emp_name; // here we Initialize the ’emp_name’ with the value passed in it

this.age = age; // here we Initialize the ‘age’ employee class variable with the value passed in it

this.salary = salary; // here we Initialize the ‘salary’ variable with the value passed in it

}

// here we define empnfo() class Method to display employee class information

public void empInfo() {

System.out.println(“employee emp_name is – ” + emp_name);

System.out.println(“employee age is – ” + age);

System.out.println(“employee salary is – ” + salary);

}

// Here we define the Main class method

public static void main(String[] args) {

//  Here we create an object using the parameterized constructor for the employee class

Employee testEmployee = new Employee(“Bhavishi”, “21”, 9999); // Here we pass the parameters to the constructor.

//  Here we call the method to display the details of the testEmployee class information.

testEmployee.empInfo();

}

}

Parameterized constructor in a Java explanation.

  • Here we declare a parameterized constructor in the Employee class, which by default accepts three parameter values: emp_name, age, and salary from the Java user.
  • When a Java user creates an object testEmployee with the method new Employee(“Bhavishi”, “21”, 9999); , the constructor is called automatically with arguments, which automatically initializes the instance variables in the Java Employee class to the given values.
  • It then prints the values ​​of emp_name, age, and salary in the empInfo() method of the Employee class.

Default Constructor vs. Parameterized Constructor in Java.

Default Constructor in Java.

  • The default constructor in a Java class program does not accept any arguments.
  • The default constructor initializes the object defined in the class program with either Java’s default values, such as 0, false, or null, in the explicit order in the constructor.
  • If a Java class does not have a default constructor defined, the Java program provides a default constructor without arguments.

Parameterized Constructor in Java.

  • A parameterized constructor in a Java program helps a Java programmer create a new class object instance using a defined and declared parameterized constructor method.
  • It accepts parameter input values ​​to allow for customized initialization of a class object in a Java program. This feature is useful when Java programmers want to initialize a class object with specific values.

Leave a Reply