Knowledge Builders

what happens if you keep a return type for a constructor

by Jacinthe Langworth Published 3 years ago Updated 2 years ago
image

As we discussed in previous questions that we can overload a constructor so if we keep return type for a constructor it will be treated as a normal method. Note: Compiler gives a warning message that method has a constructor name. Example

If we add a return type to a constructor, then it will become a method of the class. This is the way java runtime distinguish between a normal method and a constructor.

Full Answer

Can a constructor have a return type?

Constructor must not have a return type. By definition, if a method has a return type, it's not a constructor. A constructor is used in the creation of an object that is an instance of a class.

What is the return type of a void constructor in Java?

Constructor should not have any return type plus it does not return any value. If you specify a return type to the constructor then it is no different than a concrete method, you have to invoke this “void constructor” explicitly with the instance of the current class. But Java introduced this concept named constructor for a specific reason.

What happens when you create an object with a constructor?

What actually happens with the constructor is that the runtime uses type data generated by the compiler to determine how much space is needed Constructors have no return type. When you create a object so constructor will automatically call according to which object is call. Because constructor can also overload.

What happens if the programmer doesn’t write a constructor?

If the programmer doesn’t write a constructor the compiler writes a constructors on his behalf. If 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.

image

Can we have a return type in constructor?

No, constructor does not have any return type in Java. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name.

Is the return type of a constructor void?

Constructor is not like any ordinary method or function, it has no return type, thus it does not return void. Constructors don't create objects.

Why is it not necessary for constructors to have return types?

Constructor is used to initialise members of class and does not return any value that is why constructor does not have return type.

Which type of constructor Cannot have return type?

A constructor cannot have a return type (not even a void return type). A common source of this error is a missing semicolon between the end of a class definition and the first constructor implementation. The compiler sees the class as a definition of the return type for the constructor function, and generates C2533.

What happens when a return type even void is specified for a constructor or destructor?

8.3 What happens when a return type, even void, is specified for a constructor? ANS: It is treated as a method and is not considered to be a constructor.

Why do constructors do not have return type in Java?

Constructor is internally a nonstatic method with name and void return type. It does not return anything. Internally first object is allocated and then its constructor is called. Object is not allocated with constructor itself.

Does constructor return value?

No, constructor does not return any value. While declaring a constructor you will not have anything like return type. In general, Constructor is implicitly called at the time of instantiation. And it is not a method, its sole purpose is to initialize the instance variables.

Can a constructor have void?

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.

Can constructor be void in C++?

A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class and it does not have any return type at all, not even void.

What is a void constructor Java?

public void class1() is not a constructor, it is a void method whose name happens to match the class name. It is never called. Instead java creates a default constructor (since you have not created one), which does nothing.

What's the difference between constructors and void methods?

A void method specifically does not return any data or object. Pragmatically, a constructor does not return anything.

Can constructor be static?

Java constructor can not be static One of the important property of java constructor is that it can not be static. We know static keyword belongs to a class rather than the object of a class. A constructor is called when an object of a class is created, so no use of the static constructor.

On constructor not having return type

Constructor must not have a return type. By definition, if a method has a return type, it's not a constructor.

JLS 8.8 Constructor Declarations

A constructor is used in the creation of an object that is an instance of a class. [The name must match the class name, but], in all other respects, the constructor declaration looks just like a method declaration that has no result type.

On default constructors

class Parent { Parent (int a) {} } class Child extends Parent { // DOES NOT COMPILE!! // Implicit super constructor parent () is undefined for default constructor. // Must define an explicit constructor }

JLS 8.8.9 Default Constructor

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:

JLS 8.4 Method Declarations

A class can declare a method with the same name as the class or a field, member class or member interface of the class, but this is discouraged as a matter of style.

What is a constructor in Java?

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.

Does a constructor have a return type?

A constructor doesn’t have any return type. The data type of the value retuned by a method may vary, return type of a method indicates this value. A constructor doesn’t return any values explicitly, it returns the instance of the class to which it belongs.

image

1.What happens if I keep a return type for a constructor?

Url:https://www.quora.com/What-happens-if-I-keep-a-return-type-for-a-constructor

9 hours ago What happens if you keep return type for a constructor : we can overload a constructor so if we keep return type for a constructor it will be treated as a normal method.

2.Can we have a return type for a constructor in Java?

Url:https://stackoverflow.com/questions/3501680/can-we-have-a-return-type-for-a-constructor-in-java

25 hours ago Answer (1 of 10): Constructors have no return type. When you create a object so constructor will automatically call according to which object is call. Because constructor can also overload. So it show an compiler error when you create a return type. It …

3.What is the return type of a Constructor in Java?

Url:https://www.tutorialspoint.com/what-is-the-return-type-of-a-constructor-in-java

32 hours ago  · Constructors do not have a return type. Constructors are called to create an instance of the type. Essentially what is "returned" from a constructor is an instance of that type ready for use. class parent { parent (int a) {} } class child extends parent { child (int a) { super (a); } }

4.What happens if you keep return type for a constructor?

Url:https://www.transtutors.com/questions/what-happens-if-you-keep-return-type-for-a-constructor--2306263.htm

28 hours ago  · Return type of a constructor. A constructor doesn’t have any return type. The data type of the value retuned by a method may vary, return type of a method indicates this value. A constructor doesn’t return any values explicitly, it returns the instance of the class to which it belongs. Example. Following is an example of a constructor in java −

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9