• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
John Quach
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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
 
Satyen Singh
Greenhorn
Posts: 21
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya sure ...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
technically, methods do NOT return objects. They return referenced to objects.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
Ove Lindström
Ranch Hand
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • 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 Firefox Browser Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • 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: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!