Welcome! In this lesson, we will be exploring Java's constructor, a key element in object creation. Imagine creating cars on an assembly line, each painted a particular color, assigned an engine number, and further personalized, just like an object in Java. Today, we'll decipher constructors, which are essential to Java.
A constructor in Java is a specific method that initializes objects. It shares the same name as the class and doesn't return any type. If there are no user-defined constructors, Java provides a default one.
Constructors in Java adhere to some specific rules. The name of a constructor must be the same as the class name. Like a method, a constructor has a body enclosed between curly braces { }
. A constructor looks like this:
Java1public class Dog { 2 3 String breed; // Attribute 1 4 int age; // Attribute 2 5 6 // Dog class constructor 7 Dog(String breed, int age) { 8 this.breed = breed; // Initialize attribute 1 9 this.age = age; // Initialize attribute 2 10 System.out.println("Initialized Dog class with breed=" + this.breed + " and age=" + this.age); 11 } 12}
In this example, Dog
is a constructor, taking two parameters - a String
breed and an int
age. The this
keyword refers to the current instance. So this.breed
refers to the breed attribute of the current dog object, and similarly for age.
Constructors help us instantiate a class or create an object from a class. They set initial values for object attributes and carry out other functions that are necessary to create the object. Once a constructor is defined, it is automatically called when an object is created.
Here is an example of creating a Dog object, fido
from the Dog class using a constructor.
Java1public class Solution { 2 public static void main(String[] args) { 3 4 // Create 'fido' using the Dog class constructor 5 Dog fido = new Dog("Alsatian", 5); // Prints: Initialized Dog class with breed=Alsatian and age=5 6 7 System.out.println(fido.breed); // prints: Alsatian 8 System.out.println(fido.age); // prints: 5 9 } 10}
The expression Dog fido = new Dog("Alsatian", 5);
follows the format Class object = new Class(args);
, which creates the fido
object. It also prints the statement we left in the constructor in the previous section, showing the constructor has actually been called.
There are two main types of constructors in Java:
- No-argument constructor: A constructor that does not take any parameters. Also known as default constructor.
Java1public class Dog { 2 3 String breed; 4 int age; 5 6 // no-argument constructor 7 Dog() { 8 breed = "Unknown"; 9 age = 0; 10 } 11}
In this example, if no breed and age are provided when creating a new Dog object, the object will have the breed "Unknown" and age 0.
- Parameterized constructor: A constructor that has parameters.
Java1public class Dog { 2 3 String breed; 4 int age; 5 6 // parameterized constructor 7 Dog(String breed, int age) { 8 this.breed = breed; 9 this.age = age; 10 } 11}
Just like in our first example, the parameterized constructor takes breed and age parameters when you create a new Dog object.
Java supports multiple constructors with distinct parameters. This technique is called constructor overloading. The number and the type of parameters differentiate these constructors.
Here's a Dog
class featuring constructor overloading:
Java1public class Dog { 2 String breed; 3 int age; 4 5 // no-arg constructor 6 Dog() { 7 breed = "Unknown"; 8 age = 0; 9 } 10 11 // parameterized constructor 12 Dog(String breed, int age) { 13 this.breed = breed; 14 this.age = age; 15 } 16 17 // age-only constructor 18 Dog(int age) { 19 this.age = age; 20 breed = "Unknown"; 21 } 22}
In the main class, we will call both constructors like this:
Java1public class Solution { 2 public static void main(String[] args) { 3 Dog dog1 = new Dog("Husky", 3); 4 System.out.println(dog1.breed); // prints: Husky 5 System.out.println(dog1.age); // prints: 3 6 7 Dog dog2 = new Dog(10); 8 System.out.println(dog2.breed); // prints: Unknown 9 System.out.println(dog2.age); // prints: 10 10 11 Dog dog3 = new Dog(); 12 System.out.println(dog3.breed); // prints: Unknown 13 System.out.println(dog3.age); // prints: 0 14 } 15}
Great job! We've explored constructors - their concepts, syntax, usage, and types, and had a brief look at constructor overloading. Now, let's reinforce your knowledge through practice. This will solidify your understanding of constructors. Happy coding!