wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes return values in methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return values in methods " Watch "return values in methods " New topic
Author

return values in methods

Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
Just wondering --

I'm reading about return values in methods, but I'm confused about something. Take this code as an example ( I've included it all, even if it isn't all necessary -- it's from Head First Java):



My question is: where is the value returned to, if that's the right way to phrase it?
Ranveer K Kumar
Ranch Hand

Joined: Sep 13, 2009
Posts: 64
GoodDog two = new GoodDog();
two.setSize(8)
System.out.println ("Dog one: " + one.getSize ());
System.out.println ("Dog two "+ two.getSize ());
one.bark;
two.bark;


here creating object name two of GoodDog

and setting the size of the GoodDog
now
System.out.println ("Dog two "+ two.getSize ());
will call two.getSize(); (will return value of size).
which is set erlier bye two.getSize();

this is simple bean (POJO) getter and setter.
Gavi jadz Grandea
Greenhorn

Joined: Sep 11, 2009
Posts: 17

Java requires that a method declare the data type of the value that it returns. If a method does not return a value, it must be declared to return void.

In your case,
setSize() method is declared with returntype void, so no return value required.

getSize() method is declared with returntype int, that's why it should return an object of an integer.

where is the value returned to?

value is returned to an intance that called it...


regards,
-Jadz
Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
So, just to be clear, return means 'compute the value of this variable (or whatever), using this method and tell me what is'. Did I get that right?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Yes, but the method doesn't know whether you ever do anything with the result. For example in this post from a few minutes ago, I have actually used the boolean return value from the addAll method, which the original code ignored.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
Notice also that the "addAll" method does something, then returns "true" or "false" to show whether that "something" worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: return values in methods
 
Similar Threads
Quick question about instance variables
Head First GoodDog code error
Two classes defined in the same source file ??
working with book, code compiiles but won't run
Doubt about private modifier