I still don't understand when should we use return methods and when should we use void methods? What's the purpose of one and another? I get the syntax difference I just can't understand the purpose of using one instead of another?
Asked
Active
Viewed 614 times
-2
-
For example, if you take a List object. The method [`add()`](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#add(int,%20E)) just add an object to your list, so there is no point for the method to return something. So it's declared as void. But if you look the method [`get()`](https://docs.oracle.com/javase/7/docs/api/java/util/List.html#get(int)), you expect it to return the objet at this index, so the method is declard with a return type – vincrichaud Mar 15 '19 at 13:03
-
It depends if your method needs to return a value or not. – Henry Mar 15 '19 at 13:03
2 Answers
0
Some methods have to provide a result. that's when you use a return value. Example would be subtraction, addittion, validation, ...
Some methods just do something but don't have to provide a result. that's when you use void. Example would be logging, sorting, ...

Philipp Sander
- 10,139
- 6
- 45
- 78
0
When you do some work
and get the result
back from the 'some work'
, define a function and write you business logic to generate result, and return the 'result'
back, We use function/method that can return the result.
and when we want to do some work
and do not want the result
back , we use return type as void
suppose
public int add(int a,int b){
return a+b;
}
int sum = add(10,5);
//sum= 15
enter code here
printResult(sum)
The below method is not generating any result, it is just printing the value of sum
, hence, declared as void;
public void printResult(int sum){
System.out.println(""+sum);
}

Ravi Rajput
- 539
- 4
- 12