• Post Reply Bookmark Topic Watch Topic
  • New Topic

void basics  RSS feed

 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've had a Google and I'm sure that there's something very basic that I'm missing... I feel as though I know the answer although I can't put it into words!

I read that void is used when a value is not to be returned, though as far as I can tell when I have used void there is a value returned.

So I use void if I'm writing a method like :



but not :



Is it because the showName method prints to the console where as the getAge doesn't?


 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not returning anything in showName(), just printing out something in the console. There is no value returned.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can call the getAge method like this:



a value is returned from the call - assuming it is talking about me, it would return 45. You can think of that value as replacing the "getAge()", giving you this:



Java is strongly typed, which means you can't do this:



because Java knows that getAge returns an int, and that fubar must refer to a SomeOtherObject.

a void method doesn't return anything. that doesn't mean it DOES nothing. it just doesn't return a value/object back to wherever it was called.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Philips wrote:Is it because the showName method prints to the console where as the getAge doesn't?

Not really. The latter method returns something because you asked it to - it's implicit in the method name (and a good reason why you should always think carefully about what you call a method).

What value could you possibly want from a method that prints something out to the console? I suppose you could return a boolean to say whether it worked or not, but since the chances of it returning false are virtually nil - and would probably involve an Exception anyway - it seems a bit excessive.

That said, unless I'm writing something like a print method, or one that only changes the state of an object, I tend to avoid methods that return void if I can; but it's basically a style choice.

It's possibly worth adding that there's nothing in the Java language that requires you to use the value returned from a method, so you can code the call to one that does exactly like one that returns void.

Winston
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm OK, so you wouldn't see



the string part - because that would create a variable and therefore a value, which void isn't?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Philips wrote:Hmm OK, so you wouldn't see [as] the string part - because that would create a variable and therefore a value, which void isn't?

Again: no. And BTW, that method won't compile, because you've specified TWO return types.

That's not to say you couldn't code it that way. Indeed, you might actually gain some flexibility from something like:which you might then call with something like:
System.out.println( name() );
elsewhere in your class.

HIH

Winston
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mike Philips wrote:Hmm OK, so you wouldn't see [as] the string part - because that would create a variable and therefore a value, which void isn't?

Again: no. And BTW, that method won't compile, because you've specified TWO return types.

That's not to say you couldn't code it that way. Indeed, you might actually gain some flexibility from something like:which you might then call with something like:
System.out.println( name() );
elsewhere in your class.

HIH

Winston


I was referring to the



part really - having void and variable type String on the same method
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Philips wrote:I was referring to the ... part really - having void and variable type String on the same method

Not allowed. It's either void or some return type.

Winston
 
Mike Philips
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mike Philips wrote:I was referring to the ... part really - having void and variable type String on the same method

Not allowed. It's either void or some return type.

Winston

cheers
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!