• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method types  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to create a method (show below) but want it to return something - rather than use boolean or string is there anything else I could use, my method doesn't use anything specific hence why I'm wondering if there's a different type I can use rather than String,boolean or void. It's something that has always made me wonder. I want to return something but I use a mixture of booleans,strings and so forth within my method. Thanks to anyone who can shed some light on this!


 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object can stand in for anything. But then you will have to work with ... instanceof ... to make sense of the result.

In most cases I would consider a method that can return various types a code smell - which is to be investigated and possibly removed.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf thanks for the reply, but what do you mean by :

' that can return various types a code smell - which is to be investigated and possibly removed.'

I have never heard of 'code smell' before what does that mean?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:I have never heard of 'code smell' before what does that mean?


First hit on Google for code smell: http://en.wikipedia.org/wiki/Code_smell
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a well-designed program, classes and methods should have a clear responsibility - their purpose should be to represent or do one specific, clear thing.

If you are thinking about writing a method that can return completely different things under different circumstances, such as a String or a boolean, then that is a sign that your method is not designed to do exactly one, clearly defined thing. It will probably be better to create two methods - one that does whatever is necessary and returns a String, the other one that does some other action and that returns a boolean.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:I'm wondering if there's a different type I can use rather than String,boolean or void. ... I want to return something but I use a mixture of booleans,strings and so forth within my method.

I don't know if everyone is on the same page here. Did you mean you want your method to return various types of values or did you want to know if methods can return something other than String, boolean, or void?

If you want to know if your method can return various types, it depends on what you mean by "various". If you declare a supertype as the return type for your method, your method can return an object of that class or subclass. You have to be careful to make sure that that class hierarchy is designed for inheritance (a discussion that will probably require a thread of its own). In particular, the design should conform to the Liskov Substitution Principle. Basically, you take advantage of polymorphism and the fact that if, for example, you have a hierarchy like Animal <- Mammal <- Cetacean <- Dolphin <- etc. and declare a method public Animal getAnimal(), it can legally return anything that's in that hierarchy.

If you want to know if you can return anything other than String, boolean, or void, obviously you can return any other type you want. As others have already noted, you can only declare one return type though, regardless of the types of variables you deal with inside your method.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the information - I went with a string in the finish as it was suitable for my method I needed.

I was just wondering outside of the obvious boolean,string,void etc what others are there?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
really, it is limitless.

you can have your method return any primitive type or any object type you want. I could define a class called 'superNinja' and then write a method that returns that. Or I could have a method that returns a databaseConnection, or a Dog...literally anything.

You can also define your method to return an interface type. This is actually preferred. This lets someone change the method and the specific underlying object, but as long as that object implements the interface, it is still legal and nothing 'breaks'.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!