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.
2 Answers
+ 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"
0
Thanks. I think it makes sense now.