• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

missing return value error  RSS feed

 
Greenhorn
Posts: 8
Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am trying to return the value SavingsAcc in this coding exercise. I want to do this so that I can put together multiple methods in a single line. What do I need to do to fix it?

Here is the code;


here is the error:



SavingsAcc2.java:29: missing return value
return;
^
SavingsAcc2.java:35: missing return value
return;
^
SavingsAcc2.java:41: missing return value
return;
^
3 errors


I am doing all of this from the command line I plan on using eclipse or DR java soon but I just don't know them yet.

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method declarations such as "public SavingsAcc2 addInterest(double interest)" indicate a type of "SavingsAcc2" is expected to be returned. The return statement is not returning anything.

Not sure what the intent is. If you do not want anything returned, you can delcare void as the return type "public void addInterest(double interest)". Otherwise, you'll need to return an instance of SavingsAcc2 as it is currently coded.
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for the error is because your methods:



specify a return type "SavingsAcc2" but don't return a value of that type. In fact, they don't return a value at all of any type. Either change the return type to void, meaning you won't be returning anything, or provide a value of type SavingsAcc2 in your return statement.

Other pointers
- indent your code properly.
- a method will return when it is done, a "return;" is not required.
- I don't know what you mean by "so that I can put together multiple methods in a single line." Can you show an example?
 
Author and ninkuma
Marshal
Posts: 66786
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Greg Brannon wrote:- I don't know what you mean by "so that I can put together multiple methods in a single line." Can you show an example?


I believe he's talking about "method chaining".

If the methods all return this, you can do things like:
It's really common in JavaScript, but not so much in Java APIs for some reason.

I frequently wish that the JavaBean standard had specified that setters return this. It'd make code so much terser.

If that's the intent, then all those return statements should be:
 
Brian Burress
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,

Thanks for the comments. I did not understand what what was envisioned either. Returning 'this' would solve the compile errors. While this would be 'legal' in a java sense, I question the value of coding methods to operate this way as you are limiting yourself to be able to do it only with methods which do not return anything.

If there is a need for the user to perform many method calls at once, perhaps a helper method which invokes them all would be a better approach. I digress from the main question a little, sorry!!
 
Daniel Brackett
Greenhorn
Posts: 8
Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bear Bibeault! Success! and yes I was talking about method chaining!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!