Java Constructors:
Java constructors are a special type of method that is automatically called when an object of a class is created. They are used to initialize the instance variables of an object and set default values for them. Constructors are an essential part of object-oriented programming and are used to create well-designed, organized, and efficient code.Constructors have the same name as the class and do not have a return type. They can be defined with or without parameters, which allows for different objects of the same class to be initialized with different values. Additionally, constructors can be overloaded, meaning that multiple constructors can be defined with different parameters in the same class.
In this blog post, we will explore the basics of Java constructors and their role in object-oriented programming. We will also look at examples of how to define and use constructors in Java. Understanding constructors is an important step in mastering Java and creating high-quality, efficient code.
Example 1: Java Constructor:
class Main { private String name; // constructor Main() { System.out.println("Constructor Called:"); name = "learn with atharv"; } public static void main(String[] args) { // constructor is invoked while // creating an object of the Main class Main obj = new Main(); System.out.println("The name is " + obj.name); } }
Output:
Constructor Called:
The name is learn with atharv
Notice the statement of creating an object of the Main class.
Main obj = new Main();
Hence, the program prints the value of the name variables as learn with atharv.
Types of Constructor:
There are two main types of constructors in Java:- default constructors
- parameterized constructors.
1) Default Constructors:
A default constructor is a constructor that takes no parameters. It is automatically provided by the Java compiler if no other constructors are defined in the class. The default constructor sets all instance variables to their default values (0 for numeric values, false for boolean values, and null for reference types).
class Student { int id; String name; // Default constructor Student() { // This constructor takes no parameters } }
2) Parameterized Constructors:
A parameterized constructor is a constructor that takes one or more parameters. It allows objects of the class to be initialized with specific values. Parameterized constructors are defined by the programmer and can be used to perform operations such as data validation or setting up connections to databases.class Student { int id; String name; // Parameterized constructor Student(int i, String n) { id = i; name = n; } }
In Java, it is possible to have multiple constructors in the same class with different parameters. This is called constructor overloading and allows objects of the same class to be initialized in different ways.
Before we go to constructors overloading lets see some important notes on java constructors.
Important Notes On Java Constructors:
- Constructors are automatically called when an object of a class is created.
- Constructors have the same name as the class and do not have a return type.
- If a class does not have a constructor, the Java compiler will automatically provide a default constructor.
- Constructors can be defined with or without parameters, allowing for different objects of the same class to be initialized with different values.
- Constructors can be overloaded, meaning that multiple constructors can be defined with different parameters in the same class.
- Constructors can be used to initialize instance variables and perform other operations such as data validation or setting up connections to databases.
- Constructors cannot be inherited.
- Constructors cannot be overridden.
- Constructors cannot be called directly. They are only called when an object of the class is created.
- Constructors cannot be abstract, final, static, or synchronized.
Constructors Overloading in Java:
class Student { int id; String name; // Parameterized constructor 1 Student(int i) { id = i; } // Parameterized constructor 2 Student(int i, String n) { id = i; name = n; } } public class Main { public static void main(String[] args) { // Creating an object of the Student class using constructor 1 Student s1 = new Student(1); System.out.println("Student ID: " + s1.id); System.out.println("Student Name: " + s1.name); // Creating an object of the Student class using constructor 2 Student s2 = new Student(2, "John"); System.out.println("Student ID: " + s2.id); System.out.println("Student Name: " + s2.name); } }
In the main method, two objects of the Student class are created, s1 and s2. The first object is created using the first constructor Student(int i) and the second object is created using the second constructor Student(int i, String n). The values of id and name are printed to the console, showing that the correct constructor was called for each object.
Constructor overloading allows for greater flexibility and reusability in Java programs. By defining multiple constructors with different parameters, objects of the same class can be initialized with different values in different situations, making your code more versatile and adaptable.