Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why doesn't the following code produce syntax errors?

 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply change the name of one of the constructors to something other than the class name.
 
Tarun Mohandas
Ranch Hand
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that means you didn't try it. Take a minute or two, try it, and see what happens.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 33
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic