Help coderanch get a
new server
by contributing to the fundraiser
  • 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

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 ]
 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic