Question:
Can a void method have a return statement?
cfill02
2009-05-26 22:08:22 UTC
I'm pretty new to this whole Java thing and started class a while ago.
I am confused of the question asked above (in the title part)...I was reading and some says no, some says yes so I am a little confused on how it is supposed to work. Reading this Java book is just making me even more confused so if anybody can make a simple, easy explanation of it, it would be greatly appreciated!
Also wanted to know why would you include one in a void method? (if you are not able to..one of my homework questions asked me this. I think this is the reason why it has confused me.)

Thanks for the help!
Seven answers:
Suvidha A
2009-05-26 22:40:36 UTC
Though void means that it does not return a value.... if we are defining our function as void that means we dont want it to return values to us. But in some cases, we can have an if loop inside the function which checks whether it is valid to continue or not, in that case if it is invalid then we can write the statement return without any values... this will make the function go to the end of the method and return as a failure. As it is not returning any value we do not need to write the function as an int.

public void check()

{

if(argv < 2)

{

return;

}

..... normal functioning

.....

..

}

this checks for the command line argument, if the no of arguments are less than 2, there can not be any functioning, in this case program needs to be terminated....
2016-02-29 05:47:31 UTC
All methods in Java have a 'return' type. You can imagine that if a method in Java returns something it's as if that method was a sum and the returned value is the answer to that sum. A method with a return type of String returns a String, a return type of int returns an int etc. So say if a method is declared as: public static long getVector(long x1, long x2, long y1, long y2){ ... } then you know that it will return a long. You can capture the "answer" (return value) of this method by calling it like this: long myVariable = getVector(1,2,3,4); The return keyword generally comes at the end of the execution path of a method (though be aware this isn't always the last line of code in a method) and marks the value you wish to return; For example public static String getAString(){ String s1 = "what is"; String s2 = "a String?"; return s1; } Will return the String s1, the value of which is "what is"; A function declared with a void return type, eg public static void Main(String[] args){ ... } returns nothing. Void methods do not need to have a return statement (although they may have, but will simply be 'return;' and mark the end of an execution path in the method, returning program flow to the method's invocation point). Note the following: 'void' in Java is different from 'Void', which is a placeholder class for the basic data-type of 'void' - it is very rare you will ever need to use 'Void' in Java unless you are performing complicated Reflection stuff. 'void' is very different from 'void' in C/C++, which can be used in pointer arithmetic to determine pointers to undefined types and other such vagueries.If this means nothing to you now don't worry about it, but know that if you ever deal with C/C++ you'll need to spend a little more time researching what void is in that language.
vminfant
2009-05-26 22:23:02 UTC
Hi buddy... you cannot return a value from method whose type is declared as void.. because at the time of declaration itself you are telling this method will not return any value. then how can you use return statement in this...
Murali
2009-05-26 22:14:01 UTC
Yes,



Void Method () {

// do something

return;

}

Above code is perfectly correct. only diff is u dont return anything, just an emoty return
walmeis
2009-05-26 22:22:10 UTC
Sure it can. It can't return a value is all.



public void func()

{

if (something_bad)

return;

/** regular processing **/

...

}
korgrue
2009-05-26 22:15:19 UTC
No, Void means that is does not return anything to my knowledge. Least in most languages...
?
2016-09-11 14:00:24 UTC
It's possible yeah


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...