문제

(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