I know that constructors can�t have a return value. Then I read this question from Osbourne�s Java2 Study Guide:
I thought it wouldn�t compile because of line 4 and 6 but the book says I�ll compile and run fine because "you can declare a method with the same name as the class name". So... What�s the difference between constructor and method with the same name as the class name? Thanks!!!
Originally posted by Libania Paes: What�s the difference between constructor and method with the same name as the class name?
You already pointed out the difference. The difference is that a constructor doesn't have a return type. If a method with the same name as the class has no return type, it is a constructor. However, the second you add a return type to it, it becomes a normal method. Corey
The method "public void Main" is just another method that can be called. They will try to trick you into thinking it is the constructor on a lot of questions, especially with inheritence questions. Just remember that it will compile and to always look out for return types in constructor signatures... Hope that helps some.
Originally posted by Nate Johnson: The method "public void Main" is just another method that can be called. They will try to trick you into thinking it is the constructor on a lot of questions...
Nate - be sure you know the difference between a constructor and an entry point method (the "main" method). A constructor is a method that has the same name as the class, such as Object or StringBuffer. An entry point method always has the name "main." They are two entirely different beasts. Corey
Yeah I understand... I was just quoting part of the code and Main happened to be the name of the class. The point that I wanted to make was that you should watch out for return types, which shouldn't be there, in constuctor declarations on the exam. Sorry for the confusion. [ August 02, 2002: Message edited by: Nate Johnson ]
Thanks everybody! But I�m still confused... How does the code "know" if the method is a constructor or if is just a method with the same name? And seconde: what�s the point on naming a method the same as the class? When you call it, it�ll be something like Name.name();?
posted 17 years ago
First of all, the compiler can recognize a constructor by a couple details: 1. The method has the same name as the class. 2. The method has no return type. If the method meets both of these requirements, it is recognized as a constructor. If either one of the requirements is not met, the method is not a constructor - it is just another method. Secondly, there probably isn't much use in naming a method the same name as the class (apart from constructors). The key is that you can name a method the same thing as the class. It's not really good practice to do so. It would make your code more confusing because, in general, when people see a method that has the same name as the class, it is presumed that the method is a constructor. When studying for the SCJP, however, you'll encounter many instances in which you need to know what Javacan do, not necessarily what you should do with Java. I hope that helps, Corey