Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors Make me Fooollllllll they have a return type

 
Hussain
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
as everybudy knows tht constructors don't return anything even void and it is return in every java book (FACT!)
but whts tht .........
public class G111{
G111(){
System.out.println("A");
return ;}
}
i compile this and its compile as we all know tht if the method is void the compiler implicitly insert return in to it ....
my Question is if the construtors doesn't return anything so why it compiles(code above) and why all books say this non-sence tht the constructor doesn't return anything even void why?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
read this JLS 8.8.5
In fact, it is not illegal to put a return statement in a constructor, it just means that the rest of the constructor will not be executed, but the object can still be built.
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic