aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors Make me Fooollllllll they have a return type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors Make me Fooollllllll they have a return type" Watch "Constructors Make me Fooollllllll they have a return type" New topic
Author

Constructors Make me Fooollllllll they have a return type

Hussain
Ranch Hand

Joined: Nov 22, 2001
Posts: 110
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?

Muhammad Hussain<br />Sun Certified Java Programer (SCJP2)<br />Sun Certified Web Component Devloper SCWCD
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors Make me Fooollllllll they have a return type
 
Similar Threads
Constructors can Return
Another One of Those "Symbol Not Found" Questions
constructors in java
Question on Inheritance syntax
should abstart have returntrype compulsoryly