• 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

2 questions: Can a method return an object? And what does it mean when a method returns nothing?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- Can you call a method that instantiates an object and returns that object?

- What does it mean when a method returns nothing? In other words, the only word on that line is
 
Greenhorn
Posts: 21
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes ofcourse a method can instantiate an object and can return it .

a statement

return;

in a function means exit from the function without further execution.

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

Satyen Singh wrote:

return;

in a function means exit from the function without further execution.



Ah. That would be very useful in conditionals would it?
 
Satyen Singh
Greenhorn
Posts: 21
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya sure ...
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Quach wrote:- What does it mean when a method returns nothing? In other words, the only word on that line is


It means that the method was defined to return a void (ie, nothing); otherwise it wouldn't compile.

A classic example is:
public static void main(String[] args) {...

Winston
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
technically, methods do NOT return objects. They return referenced to objects.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and they are called methods, not functions. In fact, to be pedantic, it is must return a value to be called a function.
 
John Quach
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to make use of this code? I can't seem to get a main class to work with it.

 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Quach wrote:
Ah. That would be very useful in conditionals would it?



Not if you are a good programer. If you have more than one return-statement, then you should have a really, REALLY good reason.
 
Ove Lindström
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Quach wrote:Is there a way to make use of this code? I can't seem to get a main class to work with it.




Just call it.

Static methods are called Class methods and is called using the Class/Type without instantiation. So just use ReturnObject.getLabel() from your other class.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ove Lindström wrote:

John Quach wrote:
Ah. That would be very useful in conditionals would it?



Not if you are a good programer. If you have more than one return-statement, then you should have a really, REALLY good reason.



Depending on which banner of the holy war you march under. I was educated in the only-one-return school. Since then I have found that an early return can sometimes markedly improve code readability. I don't know if I would go out of my way to use one, but I wouldn't go out of my way not to, either.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dennis Deems wrote:Depending on which banner of the holy war you march under. I was educated in the only-one-return school. Since then I have found that an early return can sometimes markedly improve code readability. I don't know if I would go out of my way to use one, but I wouldn't go out of my way not to, either.


I think it may have come from the days of mainframe langauges, because I was definitely taught that way (perhaps because methods tended to be much bigger in those days); but I tend to rebel against "always do this" rules unless I can see a good reason for following it.

The classic example is a 'switch' statement: I'll always return from a switch if I can; for one thing, it saves all those darn breaks.

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic