Use Constructors in Java
In Java, the most common way to initialize an object is to use a constructor. A constructor is a method that is automatically invoked whenever an object is created. This is done by the compiler and does not involve any additional steps from the programmer. It is a special type of method which is used to initialize the object.
Constructors in Java |
{tocify} $title={Table of Contents}
Types of Java constructors
Java has two different types of constructors:
- No-arguments constructor(Default constructor): If no constructor is defined, the Java compiler will generate a no-arg constructor during execution of a program. This constructor is known as the default constructor
- Parameterized constructor: This constructor is used when you want to initialize the object with argument.
Java Default Constructor
There is a special method in Java which is called the default constructor. It is invoked when an object is created and there is no constructor with the same name. If you want to use the default constructor, you can use the following syntax:
classA myObj = new classA ();
The compiler will look for a default constructor in the class and if it doesn't find any, it will create a default constructor for the object. You can also initialize an object with the default constructor.
Example
class TestConstr { // default constructor TestConstr() { System.out.println("Hello"); } public static void main(String args[]) { //calling a default constructor TestConstr b = new TestConstr(); } } Output: Hello
Default Constructors in Java |
Java Parameterized Constructor
There is another method called a parameterized constructor in Java. A parameterized constructor is one that takes a set of parameters and can be invoked using a constructor syntax. You can use this method to initialize an object with specific values. For example, if you want to create a new object of the Fruit class, you can use the following syntax:
Fruit myFruit = new Fruit("Orange", 6, 1.5f);
The syntax above will invoke the Fruit constructor with the parameters of "Orange", 6 and 1.5.
Example
class Test { int id; String name; // parameterized constructor Test(int i, String n) { id = i; name = n; } //method to display the values void show() { System.out.println(id + " " + name); } public static void main(String args[]) { //creating objects and passing values Test s1 = new Test(235, "Abc"); Test s2 = new Test(125, "Xyz"); //calling method to display the values of object s1.show(); s2.show(); } } Output: 235 Abc 125 Xyz
Rules for creating Java constructor
- You can't use the same name for both a constructor and a method.
- constructor name must be same as its Class name
- A Java constructor cannot be abstract, static, final, and synchronized
- You can't invoke a constructor directly. It must be invoked using the object's new keyword
It is recommended to use parameterized constructors when you are creating an object with some default values and the user can specify the other values. You should also use parameterized constructors when you are passing an object as a parameter to another method. If you are using a default constructor, it is not recommended to use a parameterized constructor.
Constructors Overloading in Java
Constructors in Java can be overloaded. An overloaded constructor is one that has more than one signature. You can overload a constructor if the constructor takes different parameters of the same type. For example, if you have a constructor that takes a string, integer and float parameter, you can overload it by passing different parameters of the same type.
Example
class Test { int id; String name; int age; //creating two arg constructor Test(int i, String n) { id = i; name = n; } //creating three arg constructor Test(int i, String n, int a) { id = i; name = n; age = a; } void show() { System.out.println(id + " " + name + " " + age); } public static void main(String args[]) { Test s1 = new Test(156, "abc"); Test s2 = new Test(256, "xyz", 25); s1.show(); s2.show(); } } Output: 156 abc 0 256 xyz 25
why do Constructors not a return values?
A constructor is not a return value because it doesn't return anything. It returns an object. It is a special type of method which is used to initialize an object.
when the constructor exits, the runtime returns the newly-created instance. So the reason the constructor doesn't return a value is because it's not called directly by your code, it's called by the memory allocation and object initialization code in the runtime.