11. Using Methods II

class Dog{
  int age;
  public Dog(int dogsAge){
    age = dogsAge;
  public void bark(){
  public void run(int feet){
    System.out.println("Your dog ran " + feet + " feet!");

	public static void main(String[] args) {
    Dog spike = new Dog(13);


This is the provided code for the example. In this exercise, void is defined as “keyword (which means “completely empty”) indicates to the method that no value is returned after calling that method.” I truly don’t understand its purpose despite the explanation. Please explain what function void serves in general and what it does in this example.


The above System.out.println() returns nothing, hence the method returns nothing. Remove the keyword and run to see what, if any error is raised.


It is just execution, does not return anything.
For example,

public void printString(String str) {

public String stringMethod(String str) {
return str;

The first method does not return any value, but prints str to console

The second method returns str…
See if this helps,


There isn’t any error, and the screen does return the message in the System.out.println. It won’t work if I take out the word void, which i don’t understand because void returns nothing right? Also, what’s the point of even having a keyword that undermines your code and returns nothing? So much confusion here.


void does not return anything. The keyword is a declaration that the method returns nothing. In its abscence, Java must expect and prepare for a return value, which is why it doesn’t work when the keyword is missing.


What exactly does returning mean? Is it different from just displaying a message on screen, which is all that the “bark method” does?


A function call is like a transaction between two parties. One party is referred to as the caller, the other, the callee. When the callee sends data back to the caller, it is called a return value. When there is no data sent back, then there is no return value, and hence no return, so the function must be declared void of return.


Okay. I really appreciate your help, but there’s just one more thing. Is the “Woof” that is printed on the screen when I run the code not a return? I thought it was since it is the message being given to me. Does return mean that something is stored?


No, it is not. It is sent to the I/O buffer, not returned to the caller.


What’s the difference between what the void functions are doing and what the return functions are doing? They are both made to display a message to the user, so what is the difference?


A function declared void has no expected return value. It runs and terminates. A function not declared void will be expected to have a return, no matter what it is. If there happens to be some printing done in the function, it will have no bearing. There must be an explicit return.


So what’s the difference between printing and returning? I see a message either way, correct?


But what can you do with the message once you see it, if the value is not returned?


So if it’s returned, does that mean that I can access that message in a different function by referring to that function later.


Not the function, the return value that would have been stored. Refer to it as often as you need.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.