This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why doesn't the following code produce syntax errors? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why doesn Watch "Why doesn New topic
Author

Why doesn't the following code produce syntax errors?

Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

I read constructors are not supposed to have return types. But when I compile the following code it compiles fine.



How does the constructor have return types? How does this code work at compilation time and runtime (if we add a main method)? Can anyone walk me through the compilation please?
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8

The SimpleTypeName in the ConstructorDeclarator must be the simple name of the class that contains the constructor declaration; otherwise a compile-time error occurs.

In all other respects, the constructor declaration looks just like a method declaration that has no result type.


If it has a return type it is not a constructor.

I am moving this to the Beginning Java forum for you.


[How To Ask Questions][Read before you PM me]
Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33



The SimpleTypeName in the ConstructorDeclarator must be the simple name of the class that contains the constructor declaration; otherwise a compile-time error occurs.



Could you please give me an example of declaration that causes a compiler error?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Simply change the name of one of the constructors to something other than the class name.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

Bear Bibeault wrote:Simply change the name of one of the constructors to something other than the class name.


But that becomes another method in that class. How does that produce compile time error?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I assume that means you didn't try it. Take a minute or two, try it, and see what happens.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1646
    
    7



If you change any of the constructors to have something other than the class name it will result in a compilation error.
Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

Thank You all! I understood. It is just another method in the class if it has a return type.

That was very helpful. Thanks all
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why doesn't the following code produce syntax errors?
 
Similar Threads
Covariant returns
final variables and Conversions
Assigning fractions to primitive integral/decimal types?
casting question
Cloneable and deep copy.