1
votes

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 are 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...

2
Hmm...looks like I've confused a class vs an instance. Perhaps a better question would have been -- Is there an abstract class in the standard class hierarchy which defines only a static main() from which I should extend all of my classes that have a main() ?Paul
still, it does seem like there should be something like this.Matthew Willis
Java in a Nutshell, 5th ed., p. 135 interfaces.... have only abstract methods... and an abstract method can not be static.Paul
Once upon a time I remember "public class MyApp extends Application {..." as being in the "Hello, World" example of a java book I put down. Came away with the impression that to get anything done in Java you had to memorize some giant class hierarchy. I wonder if Application was standard back then?Paul
@Paul: You may be thinking of Applet, which you needed to use if you wanted your Java code to appear in a web browser.Greg Hewgill

2 Answers

11
votes

Interfaces can't define static methods. There is no interface that defines a main method.

3
votes

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.