Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaring a return type on a constructor doesn't cause a compilation error

I was writing code in Java and, absent-minded, i typed at some point public void BaseStation() as the constructor to a BaseStation class. To my surprise, this did not cause a compile-time error and the program got to run. Why is that? Is there any reason for someone to have a return type to a constructor?

like image 652
nikos Avatar asked Feb 21 '23 02:02

nikos


1 Answers

Specifying a return type makes this a method, rather than a constructor. You can have a method with the same name as the class (though this conflicts the conventions and is bad for readability)

like image 112
Bozho Avatar answered Mar 29 '23 23:03

Bozho