36 AnswersNew Answer
36 AnswersNew Answer
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
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...
Constructors doesn't have any return type not even void
no OR yes = true
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 :-)
It returns nothing, not even void.
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.
Void means the method returns nothing. Having no need for a return value.
We ask this q. When we get error while using can constructor in wrong in of ide So be un happay
Namit Jain Why ?
No. Constructor has no return type not even void.
No, constructors do not return anything. But, it doesn't means it returns void...nothing means nothing.
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.
Actually constructor returns instance of class that it is declared, so it is method returns it's class.
Sololearn Inc.4 Embarcadero Center, Suite 1455
Send us a message