• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Cosntructor question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Nate Johnson
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Libania Paes
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Java can do, not necessarily what you should do with Java.
I hope that helps,
Corey
 
Libania Paes
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prefect!!!
Thanks, now I got it!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In addition to the requirements I stated, from the JLS:


Unlike methods, a constructor cannot be abstract, static, final, native, strictfp, or synchronized.


Take a look at the JLS, §8.8 Constructor Declarations for more details, including the generic form of a constructor.
Corey
 
Libania Paes
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again!
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Libania,
One of the new topic specific exams on my mock exam site covers constructors. Please click on the URL in my signature.
 
Libania Paes
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
I took a look at the questions and I liked very much your site. Thanks for the tip!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic