简体   繁体   中英

Java - Interfaces, methods need to return something?

(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"!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM