(1st post don't bully me :D)

My question is simple, is it imperative that a method included in an interface HAS to return some value? (int, double, String etc..) Cause last time I checked I could not define a Void method in an interface, got compiling errors.

Thanks in advance! Cheers!

有帮助吗?

解决方案

My question is simple, is it imperative that a method included in an interface HAS to return some value?

No, absolutely not.

You can declare a void method in an interface, and indeed there are plenty of standard library interfaces with such methods. Runnable is a fine example:

public interface Runnable() {
    void run();
}

Note that declaring that a method returns Void is a different matter, and usually a mistake. (It's primarily useful for generic methods where you're going to return a value of type T - for example, Runnable is similar to Callable<Void>.)

其他提示

Did you use "Void" or "void"? (Use "void"!)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top