Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Objects (Retired) Harnessing the Power of Objects Methods and Constants

Void?

I know that the word void means that the object won't return anything, but what does it actually mean? What will it not return?

4 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Kevin,

imagine a method which would just print your name!

public void noValueToReturn() {

System.out.println("My name is Kevin Lee");
}
RESULT: My name is Kevin Lee

In this case, there is no returned value that you need to use somewhere else in the program. So the return type is "void"


Now, say I wanted to create a method that would take the sum of two numbers that I pass in as parameters and then be able to use that result, I would write

public static int findSum(int x, int y) {
    return x + y;
}

The returned value of the sum can be used somewhere else in the code ... the returned type is declared as int.

I hope it helps a little bit

Grigorij

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

It will not return a value. Some functions return things as a result of some calculation, like a number, or a string. Other functions just do something, like print to the console, but don't actually return that value. The reason functions return a value is that then you can put it into a variable or do other things to that value. But not all functions return something, which is what the void keyword is for. Someone else might be able to explain this better.

Thank you Grigorij and Brendan for those very specific answers!!! It has helped me a lot!!

If you add the void keyword , it will return void, which is a fancy way to say nothing.