可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I know that main() can be overloaded in a class with the compiler always taking the one with String[] args
as arguments as the main method from where the execution starts. But is it possible to declare the same
main(String args[]) in an interface and implement it in different classes differently?
For example,
package test;
interface test
{
public void main(String args[]);
public void display();
}
package test;
class Testclass1 implements test
{
public void display()
{
System.out.println("hello");
}
public static void main(String[] args)
{
test t;
t.display();
}
}
package temp;
import test.*;
abstract class Testclass2 implements test
{
public static void main(String args[])
{
System.out.println("TESTING");
}
}
回答1:
No you cannot, because main
has to be static in order to be used as an entry point, and Interfaces dont allow the use of static, until Java 7
.
Yes you can run a psvm
in an interface, if you're working in Java 8
. Because static methods are allowed in an interface starting from Java 8.
But of course, you cannot override the main
method, since psvm
is a static method.
回答2:
With Java-8 you can have main method defined inside the interface, Below code will work in Java-8.
public interface TestInterfaces {
public static void main(String[] a){
System.out.println("I am a static main method inside Inteface !!");
}
}
回答3:
There are two answers for your question.
- First of all, you can't have
static
methods in an Interface
- Yes you can overload
main()
method, but when you launch your class, only the
public static void main(String args[]){}
method will be treated as an entry point.
For example
public class Test {
public static void main(String[] args) {
System.out.println("entry point)");
}
public static void main(String arg1) {
System.out.println("overload2");
}
public static void main(String arg1, String arg2) {
System.out.println("overload3");
}
}
When you launch the above class, the out will be "entry point
"
回答4:
I am not sure But You can have multiple main methods in multiple classes (not the same class).
We start the program with the name of the class whose main method we want to run. So after that JVM will look for main method in that class only. So there should not be any problem.
I am not sure so please let me know if I am wrong.
回答5:
You can create you own startup mechanism, but I am not sure why you would want to.
public class RunTests {
public static void main(String... args) throws ClassNotFoundException {
List<Class> classes = new ArrayList<Class>();
try {
for (String arg : args) {
classes.add(Class.forName(arg));
}
} catch (ClassNotFoundException e) {
if (classes.isEmpty())
throw e;
}
String[] remainingArgs = Arrays.asList(args).subList(classes.size(), args.length).toArray(new String[0]);
for(Class clazz: classes) {
try {
Test test = (Test) clazz.newInstance();
test.main(remainingArgs);
test.display();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
interface Test {
public void main(String... args);
public void display();
}
BTW: You don't have to have a main() method, e.g.
class NoMain {
static {
System.out.println("Hello World");
System.exit(0);
}
}
compiles and runs without error.
回答6:
you declared a main(String[] args) as static in Testclass1,but in interface "test" it is non-static and interface not allow main(String[] args) as static .even if you override main(String[] args) in Testcalss1 as non-static it's not allowed, because main(String args) is already defined in Testclass1.so you cannot declare main(String[] args) in interface.And u done a one more mistaken is initializing the interface test as t.You cannot do that.
回答7:
main() is static.so, we cant override static methods.Interfaces allows abstract methods methods & they will be implemented in subclasses.So, abstract methods never be static.finally I concluded that main() is not possible to execute in interfaces.
回答8:
You can write static main method in Interface in java 8 but you can't override it as it's a static one.
回答9:
I think you are missing something. Static methods (like the main method in Testclass1
and Testclass2
) cannot override subclass methods.
回答10:
Answer : Yes, we can provide different implementation of main() declared in an interface and classes implementing that interface by overriding method and can overload static main method if defined in an interface.
Some more information regarding interface changes in Java 8.
Prior to Java 8, it was not possible to DEFINE methods inside interface.
But there are changes made to Java 8 with respect to interface where one can DEFINE default and static method inside an interface. Hence below code will work fine without any issue.
public interface TestInterfaces {
public static void main(String[] a){
System.out.println("I am a static main method inside Inteface !!");
}
}
For information regarding this features, please refer below link:
http://www.journaldev.com/2752/java-8-interface-changes-static-method-default-method
回答11:
this is a compiler error. you cant override a non static interface a static method