Search code examples
javaconstructorreturnprocessingvoid

"Void methods cannot return a value" - processing 3 in java


I'm working on a snake game in java, and I made a "void" function inside a snake class, that returns if the snake eats the food. when trying to execute, the console shows me "Void methods cannot return a value". how could you return a "true" or "false" inside the "void" function?

Thanks for any help! Alon.


Solution

  • By definition, void means that the function doesn't return anything. You can still return; (with no value) to end the function early, but you can't return with a value.

    To return a boolean, change void to boolean.