Learn how to create a class constructor in java programs with example and when constructors get called and about its important points.
What is class constructor in java
In brief,
In java, constructor of a class is a special method that has same name as the class name and it is automatically called when you create an object of the class. Constructor is used to initialize the object of the class at the time of object creation. There can be multiple constructors in a class having different parameters. Constructors does not have return type as methods and cannot be called explicitly.
Let’s get in detail with class constructor in java in simple language step by step.
NOTE:
[I have divided the constructor lesson in 4 posts for your better and easy understanding. First, we will understand simple constructor first i.e. how to write in a class and how the constructor gets called when we create an object of the class. Then we will understand parametrized constructor in java, writing multiple constructors in a class and then how to initialize object of the class.]
Lets start simple.
Suppose there is a class Car , then its constructor will be as below seen in example with same name of the class i.e. public Car(){}. It is similar to a method but does not have return type.
class Car {
//Constructor
public Car(){
}
}
In above class constructor example, when we create an object of the class Car, lets say, Maruti , as, “Car maruti = new Car();” then constructor will be called automatically. [ You can read more on how to create an object of a class in java here.]
You need to remember the Golden Rule given below.
Golden Rule: “Create an object of a class, the class constructor will get called automatically”
Lets dive deeper with a complete java code example.
Class Constructor and its Calling Example in java
In below java source code example, a Car class is there containing a constructor “public Car()” . When we create an object of the class Car, lets say, Maruti , as, “Car maruti = new Car();” then constructor will be called automatically.
Similarly, when we create object Honda of the class Car, then again constructor will be called. Means, As many times as you create an object of the class using new keyword, the constructor will get called every time automatically / implicitly.
Constructor Calling Java Example
/*
* Example of constructor in java
*/
class Car {
public Car(){
System.out.println("Constructor...");
}
public void run(){
System.out.println("Car is running...");
}
}
public class Sample {
public static void main(String[] args) {
//We we crate an object, the consturctor of the
//class will be called automatically.
Car maruti = new Car();
maruti.run();
Car honda = new Car();//It will also call constructor of the class
honda.run();
}
}
Output
Constructor…
Car is running…
Constructor…
Car is running…
EXERCISES-CONSTRUCTOR
EXERCISE-1:
Answer the following questions in brief.
- When a class constructor gets called?
- If you create 5 objects of a class, then how many time constructors will be called?
- When you write a constructor, what return type do you write in constructor declaration?
- Why do you use constructor?
EXERCISE-2:
Write a constructor in the Car class given below that initializes the brand class field with the string “Ford”.
Call the getBrand() method in the main method of the Sample class and store the value of the brand in a variable, and print the value.
class Car {
String brand;
//your constructor here
public String getBrand() {
return brand;
}
void run() {
System.out.println("Car is running...");
}
}
public class Sample {
public static void main(String[] args) {
Car ford = new Car();
}
}