Is the return type of a constructor void?
A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes: Show ExampleCreate a constructor:
Try it Yourself » Note that the constructor name must match the class name, and it cannot have a return type (like Also note that the constructor is called when the object is created. All classes have constructors by default: if you do not create a class constructor yourself, Java creates one for you. However, then you are not able to set initial values for object attributes. Constructor ParametersConstructors can also take parameters, which is used to initialize attributes. The following example adds an A constructor is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class. Return type of a constructor
ExampleFollowing is an example of a constructor in java − Live Demo public class Sample{ public Sample(){ System.out.println("Hello how are you"); } public Sample(String data){ System.out.println(data); } public static void main(String args[]){ Sample obj = new Sample("Tutorialspoint"); } } OutputTutorialspoint ExampleLive Demo class Student{ Integer age; Student(Integer age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } } public class GenericsExample { public static void main(String args[]) { Student std = new Student(25); std.display(); } } OutputValue of age: 25 Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class. If the programmer doesn’t write a constructor the compiler writes a constructors on his behalf. ExampleIf you closely observe the declaration of the constructor in the following example it just have the name of the constructor which is similar to class and, the parameters. It does not have any return type. First, we'll get familiar with how object initialization works in Java and the JVM. Then, we'll dig deeper to see how object initialization and assignment work under-the-hood. 2. Instance InitializationLet's start with an empty class:
Here, we're going to create an instance from this class and assign it to some variable:
After compiling this simple Java snippet, let's take a peek at its bytecode via the javap -c command:
When we instantiate an object in Java, the JVM performs the following operations:
As shown above, the method signature for the default constructor is:
The
Therefore, the return type of a constructor in Java and JVM is void. Taking another look at our simple assignment:
Now that we know the constructor returns void, let's see how the assignment works. 3. How Assignment WorksJVM is a stack-based virtual machine. Each stack consists of stack frames. Put simply, each stack frame corresponds to a method call. In fact, JVM creates frames with a new method call and destroys them as they finish their job: Each stack frame uses an array to store local variables and an operand stack to store partial results. Given that, let's take another look at the bytecode:
Here's how the assignment works:
From now on, the second item (index 1) in the local variables array is a reference to the newly created object. Therefore, we don't lose the reference, and the assignment actually works — even when the constructor returns nothing! 4. ConclusionIn this quick tutorial, we learned how the JVM creates and initializes our class instances. Moreover, we saw how the instance initialization works under-the-hood. For an even more detailed understanding of the JVM, it's always a good idea to check out its specification. What is the return type of a constructor?Therefore, the return type of a constructor in Java and JVM is void.
Does a constructor have a void return type in Java?Note that the constructor name must match the class name, and it cannot have a return type (like void ). Also note that the constructor is called when the object is created.
Does void count as a return type?In lieu of a data type, void functions use the keyword "void." A void function performs a task, and then control returns back to the caller--but, it does not return a value. You may or may not use the return statement, as there is no return value.
Can constructors return value return void?No, constructor does not return any value.
|