• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructors Make me Fooollllllll they have a return type

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic