• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

returning a value from a method.

 
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i understand how to pass a value to a method,its pretty easy...but have i got returning values from methods right?

when you want to return a value leave out the void keyword

are you always returning a calculation of something?
for example a calculation of two sums etc like age + age

is it always returned as a variable?



i still dont really understand returning a value,as if i create a variable in one class and instantiate that
i can still use its variables...can somebody please break this down into idiot speak for me so i can move along

thanks

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you don't just leave out the "void" keyword...you have to replace it with what your method will return.  It could be a primitive or an object.

You are not always returning a calculation. You might return an object that is a connection to a database. Or a String. Or just about any kind of object.

it does not have to be a variable.  If I have a method declared to return an int, i could do this:

return 7;

or a boolean method could have this:

return true;


In some ways, a method can be thought of as asking a question, and the thing returned is the answer.

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of simplest is (usually) a getter. A typical getter looks like this:
I used "int" for the type here but it could be anything.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a little program i wrote on lunch today



and i instantiate and call the getTotal method from my main in theshop class (didnt think it was necessary to post it here)

now this works...and i get the total of whatever two numbers i pass in

and the method prints out the total too,so i dont need to return the value to the main method.
what can i add to this program that would require it to return a value?

heres another one,i tried returning a value with this code:



but i just have intelisense saying invalid method declaration,return type required

im sure im mssing something obvious i prob have red 5 times...any help appreciated


 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the tool says "Return type required" it means that it is expecting a return type but couldn't find one.  On line 6, what is the return type of the method that you declared?  
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ooooh ok added int and its working fine now...

i thought because it was taking in int it would know to return it
this really makes sense to me know
if you want a return (even if taking in values) you need to specify it

also when would this program need to return something
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

when would this program need to return something?


A method must return something whenever the method's return type is declared as something other than "void".
For example,

must return an int, and

must return an instance of MyType.

it always returned as a variable?


No, it is not necessary to return a variable. You can return anything that resolves to the correct type. So instead of

You could eliminate the variable 'value' and just say
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing is that the getTotal method does not get a total. You cannot get a total from it. The method is named incorrectly; it shou‍ld be called displayTotal. Either print the total or return it. Don't do both in the same method.
 
jon ninpoja
Ranch Hand
Posts: 424
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried another little program:



heres main



but why isnt it returning? shouldnt it print out in the console the age?
i know i can print this in the method itself? but doesnt that defeat the purpose?
also i cant see any of the variables in the method...of course this is due to variable scope
so what do i have to do

i will do what somebody on here said a few posts ago...regarding something to try on a program i have already written
was just on lunch and was giving this a go....oh i hate my job...its really getting in the way

 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jon ninpoja wrote:


This doesn't print anything out and it doesn't set the "customerAge" field. It has a variable "birthYear" but this is really age.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a method has a return type, it can act like a variable of that type.  So for instance,

But there's an easier way.  You don't need to set a variable to use the return type:
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is not the birth year, nor even the age, but rather the negative of the age.
 
reply
    Bookmark Topic Watch Topic
  • New Topic