Does Constructor Return Any Value?
To find out whether constructor returns any value or not, let’s learn about a constructor? In simple terms, the constructor is a special method, which is used to initialize an instance of a class. This special method differs from the actual methods of classes. The name of a constructor is always similar to the name of the class. Below is an example of a constructor:
1 2 3 4 5 6 7 8 9 10 11 |
public class Complex { //This is the constructor Complex(){ } .. } |
How Constructor Work? Consider an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Student { String name; //Constructor Student(){ this.name = "Hello constructor"; } public static void main(String[] args) { Student obj = new Student(); System.out.println(obj.name); } } |
OUTPUT: Hello constructor The object is built
Read more