Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Static Methods  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does Java allow someone to return more than one answer or number when using the "return" function in a static method.

for example:


if (a>b && b>c)

a >b

return a;
return b;
}

can you actually do this?


Thanks
 
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you mean "can I return one thing or another from a method?" like this:

then the answer is yes.

If you mean "Can I return two things from a method?" then the answer is also yes:


If you mean "can I call return twice to return two things?" then the answer is no. The reason is shown in the first example - return will 'exit' the method.
[ March 08, 2005: Message edited by: Paul Sturrock ]
 
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another trick to use (and, yes people, feel free to tell me that this isn't a good design... ) is output parameters. In C, this is typically doen with pointers-to-pointers, but in Java, we don't have pointers. But we do have arrays.



As I say, this is generally not good programming practice. Usually, I use this as is only a temporary approach, and wind up refactoring the method call into two methods or returning a complex type down the road.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, using array types to store individual return values which must be picked off from the correct indices isn't good practice (unless it's an obvious one like coordinates or something). As Joel said, it's better to define a "complex" type that holds your values and exposes them in a more meaningful manner. Then, your API tells you what it's doing rather than requiring someone to read your documentation to figure out which index to use to get what value. Also, if you do ever need to add stuff to the return value, it's much easier, especially if you were to want to add something into the middle of the array.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam:

Look at the interface called Serializable. Here is how I would use it.

Class A implements Serializable
{
}


Class B implements Serializable
{
}

Class C implements Serializable
{

public Serializable doSomething(Object E)
{
if (E instanceof A)
return (A)E;
else if(E instanceof B)
return (B)E;

}
}

Also you can do this using the Object class. The only stipulation I would say is that you must use the "instanceof" facility.

Best of luck,

Russ
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Russ, using Serializable doesn't buy you anything in your example, you could simply use Object as return type.

Type cases (your if statements using instanceof) are a bad idea, though - they fly in the face of OO programming. It's much better to use polymorphic behaviour instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!