Java Constructors
Introduction to Constructors in Java
A constructor in Java is a special method that is used to initialize an object when it is created. Constructors have the same name as the class they belong to and they do not have a return type.
Constructors can be used to perform a variety of tasks, such as:
- Initializing the object's fields
- Validating the object's state
- Performing any other necessary setup for the object
Constructors are called automatically when an object is created using the new keyword. For example, the following code creates a new instance of the `Person` class:
Person person = new Person();
This code will automatically call the `Person` class's constructor to initialize the new object.
Java classes can have multiple constructors, but each constructor must have a unique set of parameters. This is known as constructor overloading. Constructor overloading allows you to create different constructors for different types of objects.
For example, the following code shows two constructors for the `Person` class:
public class Person {
private String name;
private int age;
public Person() {
this.name = "";
this.age = 0;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
The first constructor is a default constructor that takes no parameters. This constructor initializes the `name` and `age` fields to empty strings and 0, respectively.
The second constructor takes two parameters, `name` and `age`. This constructor initializes the `name` and `age` fields to the values of the corresponding parameters.
To create a new instance of the `Person` class using the second constructor, you would use the following code:
Person person = new Person("Alice", 25);
This code will create a new `Person` object with the name "Alice" and the age 25.
Constructor Chaining in Java:
Constructor chaining is a technique in Java that allows a constructor to call another constructor within the same class. This can be useful for providing default values, performing common initialization tasks, or creating more flexible constructor signatures.
class Person {
private String name;
private int age;
// Default constructor
public Person() {
this("Unknown", 0); // Calls the parameterized constructor
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
How Constructor Chaining works?
- this keyword: The `this` keyword is used to refer to the current object being constructed.
- Constructor call: Within a constructor, you can use `this` to call another constructor within the same class.
- Chaining sequence: The called constructor can in turn call another constructor, creating a chain of constructor calls.
- Order of execution: The chain of constructor calls is executed from the most derived class (subclass) to the least derived class (superclass).
Constructors are a powerful feature of Java that can be used to initialize objects in a variety of ways. By understanding how to use constructors, you can write more efficient and robust code.
Exercises
Code for creating an object for a class that has a default constructors
package javaprogrammingdemo;
public class javalabclass{
public static void main(String args[])
{
students s = new students();
s.display_student();
}
}
class students
{
String regNo;
String name;
//code a default constructor
students()
{
this.regNo="test";
this.name="test";
}
public void display_student() {
System.out.println(this.regNo+this.name);
}
}
Code for creating objects for a class that uses parameterized constructors
package javaprogrammingdemo;
public class javalabclass{
public static void main(String args[])
{
students s = new students("111","satish");
s.display_student();
students ram = new students("112","ram");
ram.display_student();
}
}
class students
{
String regNo;
String name;
//code a parameterized constructor
public students(String regNo, String name) {
this.regNo = regNo;
this.name = name;
}
public void display_student() {
System.out.println(this.regNo+this.name);
}
}
Code for creating objects for a class that uses Overloaded Constructors(both default and parameterized).
//constructors overloading
package javaprogrammingdemo;
public class javalabclass{
public static void main(String args[])
{
students s = new students("111","satish");
s.display_student();
students ram = new students("112","ram");
ram.display_student();
students mohan = new students();
mohan.display_student();
}
}
class students
{
String regNo;
String name;
//constructor overloading
public students() {
this.regNo="test";
this.name="test";
}
//code a parameterized constructor
public students(String regNo, String name) {
this.regNo = regNo;
this.name = name;
}
public void display_student() {
System.out.println(this.regNo+this.name);
}
}
Code for creating an array of objects for a class that has a parameterized constructor
package javaWinterSemester;
import java.util.Scanner;
public class javademo {
public static void main(String[] args) {
students k[] = new students[2];
Scanner input = new Scanner(System.in);
String regno;
String name;
for(int i=0;i<k.length;i++)
{
System.out.println("Enter the student REgno");
regno=input.next();
System.out.println("Enter your name");
name=input.next();
k[i]=new students(regno,name);
}
for(int i=0;i<k.length;i++)
{
k[i].display_arrayofobjects();
}
}
}
class students
{
public String regNo;
public String name;
public students(String regNo, String name) {
this.regNo = regNo;
this.name = name;
}
public void display_arrayofobjects() {
System.out.println(this.regNo+this.name);
}
}