0

What is the purpose of void in this question?

In a lesson on java creating classes, they gave this example: public class Animal { void bark() { System.out.println("Woof-Woof"); } } I could'nt figure out what void was used for in the program so i checked the comments. Someone else had the same question but the replies there were about what void does and not how it affects this specific code.

1st Apr 2020, 2:43 AM
Ifeoluwa David Adeyemi
Ifeoluwa David Adeyemi - avatar
2 odpowiedzi
+ 1
when you add 'void' in a method it means you don't want to return a value The purpose of the bark() method is simply to print the text "Woof-woof", so you must add 'void' because you don't want to return a value. A method like this "int bark()" means that you want to return an integer value: int bark() { System.out.print ("Woof-Woof"); return 15; // You have to return an integer } but we don't have to, because the purpose of the method is just to print "Woof-woof"
1st Apr 2020, 3:31 AM
Raúl
0
Thanks. I think it makes sense now.
1st Apr 2020, 8:21 PM
Ifeoluwa David Adeyemi
Ifeoluwa David Adeyemi - avatar