• Post Reply Bookmark Topic Watch Topic
  • New Topic

returning a value from a method.  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • 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

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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.

 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 291
3
  • Mark post as helpful
  • send pies
  • 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


 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 291
3
  • Mark post as helpful
  • send pies
  • 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
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • 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: 291
3
  • Mark post as helpful
  • send pies
  • 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: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Knute Snortum
Sheriff
Posts: 4284
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 571
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is not the birth year, nor even the age, but rather the negative of the age.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!