Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Method must return an Object  RSS feed

 
Russell Ray
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method with a signature such as this:
public Object methodName(String param, String param2)

The behavior of the method can return two types--a Boolean or a String Buffer. What I try to accomplish is declare two local variables as Objects (a or b). I create each as a Boolean or a StringBuffer.
Object a = new StringBuffer();
Object b = new Boolean();

Depending on where I am in the method, I return the Object (a or b).

if(a != nuul)
return a;
else
return b;

I am using JDK 1.4.2.

When I compile the code, I get a fatal error saying the method must return an Object.

What am I not seeing? I've tried casting the variables to Objects and still it does not work.

Any help would be appreciated.

Russ
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd have to show us the whole method before we could tell you what you've done wrong. The code you've shown would work just fine.
 
Russell Ray
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I discovered was I could go all the way through the method and still return something other than the two objects I created....
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Russell Ray:
What I discovered was I could go all the way through the method and still return something other than the two objects I created....

What do you mean?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the compiler complaining that it is returning something that is not an Object? Or is the compiler complaining that there is a case where you are not returning anything -- getting to the end of the method with no return statement?

In either case, we need to see code...

Henry
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiosity, how does the calling method know what type of Object is returned?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!