0

Why does this method return something while void is being used?

Hi guys, This is my first post, thanks in advance for any replies. I learnt that void means that a method does not return anything. How come that the method 'sayHello()' is still able to return a string? class MyClass { static void sayHello() { System.out.println("Hello World!"); } public static void main(String[ ] args) { sayHello(); } } // Outputs "Hello World!"

13th Feb 2017, 1:00 PM
indymaat
5 Answers
+ 18
It doesn't return anything to another function
 It just prints in the console


13th Feb 2017, 1:03 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 2
void tell to compiler that the method doesn't return a value. Otherwise you had to put a type of return e.g. String,int,etc... and use a return statement in the method.
13th Feb 2017, 1:35 PM
dom
+ 2
Here main method is calling the sayHello() method and that sayHello() method is not returning anything back to the main method. Hello World! is not being returned back to the main method. It will be just printed.
13th Feb 2017, 1:40 PM
Kommoju Sunil Kumar
Kommoju Sunil Kumar - avatar
0
Thank you for your answer. That's exactly the thing i don't understand. Then what is void doing here? edit: So Java acts different on returning a method (System.out.println();) than returning a value of a certain datatype. In other words; the void word is only applicable on the returning of data types and not on methods. Thank you guys.
13th Feb 2017, 1:41 PM
indymaat
0
Thanks, For now this is clear to me, thank you community!
13th Feb 2017, 1:42 PM
indymaat