Is there a standard interface guaranteeing void main(String[] args)?
I'm fairly new to java and looking for an interface that simply guarantees that a main(args) exists -- e.g. suitable for running from the command line with "java classname arg1 ... argN " -- without doing more.
More formally, I think that this would suffice:
public interface App { public static void main(String[] args); }
Is there such an interface in the standard libraries that ar开发者_如何学Pythone usually found in a JDK?
I couldn't find a formal entry for "Application" or "App" in the Nutshell book nor does googling "java interface main" turn up anything useful.
Thanks in advance...
Interfaces can't define static methods. There is no interface that defines a main method.
As others said, you can't have abstract static methods. I'll try to explain why.
A static member is attached to one class only - the one that it's defined in. It can't be inherited. The problem is, the Java syntax makes it look like you can inherit it; if a parent class A has a static method f(), and you write a subclass B, then you can call the method like this: B.f()
. However, you're actually calling A.f()
. This is a meaningless distinction, unless you do something like this:
class A {
public static String s = "a";
public static String f() {
return s;
}
}
class B extends A {
public static String s = "b";
}
Here, A.f()
and B.f()
will both return "a".
So: if you can't inherit a static method, then you can't override it; and if you can't override it, then making it abstract would be pointless.
精彩评论