why return type is not allowed for constructor? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

why return type is not allowed for constructor?

3rd Nov 2016, 1:42 PM
Abdulrhman Farahat Al-Okizi
Abdulrhman Farahat Al-Okizi - avatar
2 Answers
+ 2
As there is a possibility to define a method with same class name , return type is not allowed to constructor to differentiate constructor block from method block
3rd Nov 2016, 1:44 PM
Abdulrhman Farahat Al-Okizi
Abdulrhman Farahat Al-Okizi - avatar
+ 1
a constructor is use to creat an instance of your class. the return of a constructor will be an instance of your object that you wanted to creat. this special method will be only call when you will want to creat a new instance of an object. for exemple: String s = new String ("my New object");
3rd Nov 2016, 2:05 PM
Alexandre Gounin
Alexandre Gounin - avatar