Methods using same name as constructor
This isn't so much a problem that needs a solution, as just a request for comment (I already solved the problem):
I converted an applet to a stand-alone app by renaming init() as a constructor, and took an hour to figure out why the constructor didn't run. Of course, I didn't delete the void return type. So I ended up with:
public class A {
public void A() {}
}
This got me thinking. I can't think of a single reason why anyone would want a method A() in class A - it's too easy to confuse it with a constructor, but the compiler accepts it (JDK 1.3).
Surely "error: constructor can't have return type" would have made much more sense? I checked all my docs and can't find a reference to why this quirk is there.
Comments, anyone?
[796 byte] By [
alan.mck] at [2007-9-26 2:11:45]

OK, I see your logic, and might even be tempted to agree with it. But lets look at it this way: when the compiler parses public void A() it has to fully parse it to recognise a method. It already knows the name of the class, already has the method name, return type and accessibility. Checking for validity is one simple if statement, so your complexity claim breaks down.
It just seems to me that as Java is so particular about detail that something like this would have been disallowed in the language spec. Mind you, the spec also allows this oddity:
class A {
A A(A A) {
return new A();
}
}
.... :-)
Obscure quirk maybe, but also a source of hard-to-find errors. I've seen numerous pleadings on the new-to-Java forum, where the new Java person had written
public void Alpha(int n)...
and couldn't understand why "new Alpha(42)" was rejected by the compiler.