Zizouz212
Zizouz212

Reputation: 4998

Void methods can't return the value of a void method?

I don't mind if I don't understand, but I want to know why this happens:

void something(String a) {
    return hi();
}
void hi() {
    return;
}

The odd thing here, is that hi() also has a return type of void. I get the syntax error in my IDE:

Void methods cannot return a value

Furthermore, the code doesn't compile:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Void methods cannot return a value

    at Resources.setSystemProperties(Resources.java:33)
    at Resources.main(Resources.java:49)

I would expect this to be happening:

hi() -> return nothing

return [nothing] -> hi() is nothing

So in the end, it returns nothing, just like a void method should.

Why does this behaviour happen? And why doesn't the code compile, when return the result of a void method?

Upvotes: 1

Views: 6478

Answers (5)

Nathan Hughes
Nathan Hughes

Reputation: 96385

Because that's how the language works. You can't use the return keyword to return a value in a method that has a return type of void. void is not a real value that can be passed around:

The Void class is an uninstantiable placeholder class to hold a reference to the Class object representing the Java keyword void.

Here's an article by James Iry comparing void in Java with Unit in Scala. (Unlike void, unit is an actual value that can be returned.) He demonstrates there are advantages to having a Unit type instead of void:

Java, C++, and C# programmers do solve this problem all the time. One common option is to not use Function but some other concept like "Action" to mean a function that takes a string, performs a side effect, and returns nothing useful. Then you essentially duplicate "map" into something called perhaps "foreach" that expects an Action and returns void. That probably makes sense in this case since a list of references to a meaningless value is perhaps silly, but it also means a great deal of code duplication if this kind of higher order programming is common. For instance, you can't write just one compose function that creates a function from two other functions, you also have to write a compose that takes an action and a function to create an action.

Upvotes: 2

yshavit
yshavit

Reputation: 43391

This is defined in JLS 14.17:

A return statement with an Expression must be contained in one of the following, or a compile-time error occurs:

  • A method that is declared to return a value
  • A lambda expression

A void method is not declared to return a value, so a return statement with an expression (like a function call) cannot happen in such a method.

Additionally, you can't return the result of a void method due to this language in JLS 15.12.3:

If the compile-time declaration is void, then the method invocation must be a top level expression (that is, the Expression in an expression statement or in the ForInit or ForUpdate part of a for statement), or a compile-time error occurs.

In other words, since a.notify() is void, you can use it in contexts where it looks like a statement (ie, all alone on a line) but not in contexts where it looks like an expression (ie, you can't assign its value to a variable, return it, etc).

Upvotes: 4

Filip
Filip

Reputation: 2344

Java compiler will not go so much in depth. When it sees return keyword with some value following in void function it will throw compilation error. It is programmed so. As simple as that.

Upvotes: 0

bane19
bane19

Reputation: 392

return  statements simply cannot be used with `Void` as it's Return type.

You can't change language rules!

Upvotes: 0

Lawrence Aiello
Lawrence Aiello

Reputation: 4638

A function declared as void cannot return anything, just like the error says. Your assumption that a.notify() returns anything is false, because a.notify() also cannot return anything (since it is void).

Upvotes: 2

Related Questions