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

constuctor ---- return statement

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

can constructor have the return statement..?

thanks,
vinay rajnish
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A constructor cannot have a return type . If it has a return type , then it bcomes a method . It is an easy way to differentiate a constructor from a method.

Hope you understood the above explanation.

Originally posted by vianyrajnish rajnish:
hi,

can constructor have the return statement..?

thanks,
vinay rajnish


[ October 17, 2007: Message edited by: Rakesh Yelugoila ]
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vianyrajnish rajnish:

can constructor have the return statement..?


Yes, a Constructor can have a blank return statement, but Not return type.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello , everybody!

I have tested the following code just now.It can be compiled and ran well. You can try it yourself!

class A
{
public static void main(String[]A)
{
B b=new B();
System.out.println(b);
}
}


class B
{
int B(){return 3;}
}
In my opinion,the compiler may delete or ignore the return type and return statement! Can anbody explain that ? waiting...........
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Fire Feather:
Hello , everybody!

I have tested the following code just now.It can be compiled and ran well. You can try it yourself!

class A
{
public static void main(String[]A)
{
B b=new B();
System.out.println(b);
}
}


class B
{
int B(){return 3;}
}
In my opinion,the compiler may delete or ignore the return type and return statement! Can anbody explain that ? waiting...........



Hi Fire Feather,

I suspect you're going to get some heat about your display name...

Aside from that, in your B class:


The B() method is not a constructor because it has a return type. It is simply just another ho-hum method to the compiler. To qualify as a constructor, throw away the return type and return value:



Aloha,
Doug

-- Nothing is impossible if I'mPossible
[ October 18, 2007: Message edited by: Doug Slattery ]
 
It's never done THAT before. Explain it to me tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic