A constructor is a void type method. Yes or No? | SoloLearn: Learn to code for FREE!

+7

A constructor is a void type method. Yes or No?

OOP

why

9/24/2020 1:41:34 PM

Mae Ann Velasco

36 Answers

New Answer

+26

Because it doesn't need to return even a void value (nothing). A constructor is a special method which allocates memory (assigns variables) for the new instances of classes we make. You cannot have any other method like constructors. And if you add a return type, it becomes a normal method and it will no more be called during instantiation

+12

No. Constructor don't have return type.. If you add a return type, then it becomes normal function.. Instead of "why" in tag, pls add relevant tag like language name.. And add clear description about your doubt in description...

+7

+6

Constructors doesn't have any return type not even void

+4

no OR yes = true

+4

Constructor -- No return type -- Overloading of Constructor is possible -- We can have static Constructor -- Default Constructor is used for the instance creation -- If class contains POCO /Auto properties then default Constructor is not required/not called... There are lots of points please refer standard source for more information :-)

+4

It returns nothing, not even void.

+4

In java, Constructor is not a method .The work of the constructor looks like a void method but it's mainly not. When constructor is not present, a default constructor is provided In the case of a method, no default method is provided Constructor is used to initialize an object whereas method is used to exhibits functionality of an object. Constructor is automatically called when the class is called. See the answer of this question: https://stackoverflow.com/questions/24963718/java-void-and-non-void-constructor in c++, Constructor is a special member function of a class that initializes the object of the class. Constructor name is same as class name and it doesn’t have a return type. You cannot declare a constructor a void as it's a special function that makes different a constructor and a void function. It is recommended to have a constructor of every class if you don't create it ,it will be automatically created . Hope this helps.

+3

+3

+3

+2

Void means the method returns nothing. Having no need for a return value.

+2

We ask this q. When we get error while using can constructor in wrong in of ide So be un happay

+2

Namit Jain Why ?

+2

+2

No. Constructor has no return type not even void.

+2

+2

A constructor is of course not a void type method because a constructor returns a value or a object. Hope it is helpful😄.

+2

No, constructors do not return anything. But, it doesn't means it returns void...nothing means nothing.

+2

Technically constructors are not methods with void return type, nor are they methods with only return type and no name (my former assumption). They're special constructs in many OOP languages and they're not necessary to have working OOP language. In some languages like Ruby, instead of constructors you have classes that have methods to create instances, i. e. Animal.new("dog") vs new Animal("dog"). 'new' in Ruby is not a keyword, but a method that allocates memory for an object then calls its initialize method. Even then, they return new object and not void.