2015-08-24 56 views
-3

我刚刚开始使用Akka框架,并在documentation中看到以下代码片段。有人可以解释这段代码吗?是否允许在Java中声明顶级类为静态

它允许声明顶级类为静态吗?我发现了similar问题。它表示顶级类不能在Java中声明为静态,但在本例中顶级顶级代码已声明为静态!我错过了什么?

static class MyActorC implements Creator<MyActor> { 
    @Override public MyActor create() { 
     return new MyActor("..."); 
    } 
} 

Props props2 = Props.create(MyActor.class, "..."); 
Props props3 = Props.create(new MyActorC()); 

回答

6

NO。

只有班级成员可以是static。成员包括fields,methods,nested classes,enums等。

但是类(顶层)本身不能是静态的(而且它也不适用于静态)。

而在documentation你提到,他们有提到MyActorC顶层类,有他们? (如果您观察,导入语句在不同的代码片段中)。

1

我想你错过了这些代码片断的事实。您也不能在根级别执行Props props2 = Props.create(MyActor.class, "...");。该代码属于一种方法。

+0

是。但是如果这个例子正确地缩进了,并且显示了封闭的类声明,这将会有所帮助。 –

相关问题