Java 为什么我会出错;非静态变量不能从静态上下文中引用此变量;?

Java 为什么我会出错;非静态变量不能从静态上下文中引用此变量;?,java,static,inner-classes,Java,Static,Inner Classes,我读过关于非静态变量的内容,这不能从静态上下文中引用错误,但我不明白为什么在我的案例中会得到它(行返回新的CommandParser1(command);)?。我只是创建类的实例。这就是全部。有什么问题 public class ProtocolUtility { public static CommandParser createParser(String command) throws Exception { switch (command)

我读过关于
非静态变量的内容,这不能从静态上下文中引用
错误,但我不明白为什么在我的案例中会得到它(行
返回新的CommandParser1(command);
)?。我只是创建类的实例。这就是全部。有什么问题

public class ProtocolUtility {

    public static CommandParser createParser(String command) throws Exception {           
        switch (command) {
            case COMMAND_1:
                return new CommandParser1(command);                  
            case COMMAND_2:
               return new CommandParser2(command);
            default:
                return null;
        }
    }

   public abstract class CommandParser {

       protected String command;

       public String getCommand() {
          return command;
       }       
   }

   public  class CommandParser1 extends CommandParser {       
       public CommandParser1 (String command){
           //...
       }       
   }

   public  class CommandParser2 extends CommandParser {      
       public CommandParser2 (String command)  {
           //...
       }      
   }

}当以静态方式调用
createParser
时(即
ProtocolUtility.createParser(…)
),您不能从
ProtocolUtility
中定义的类实例化对象,因为这要求您拥有该类的实例(您没有)。当以静态方式调用
createParser
时(即
ProtocolUtility.createParser(…)
),也可以通过使内部类
成为静态的

来解决这一问题。您不能从
ProtocolUtility
内部定义的类实例化对象,因为这需要您拥有该类的实例(您没有)。也可以通过使内部类
静态
CommandParser
是一个内部类来修复此问题,这意味着它需要创建外部类(
ProtocolUtility
)的实例。将其声明更改为:

public static abstract class CommandParser {
或者在单独的
.java
文件中声明
CommandParser


如果
createParser()
不是静态的
ProtocolUtility
,那么代码也可以工作,因为在本例中,您当前所在的
ProtocolUtility的实例将用作外部实例。

CommandParser
是一个内部类,这意味着它需要一个外部类的实例(
ProtocolUtility
)要创建。将其声明更改为:

public static abstract class CommandParser {
或者在单独的
.java
文件中声明
CommandParser


如果
createParser()
不是静态的,那么代码也可以工作,因为在本例中,您当前所在的
ProtocolUtility
的实例将用作外部实例。

因为CommandParser1()不是静态的。您需要CommandParser1的实例来调用CommandParser1()或者将其定义为静态。

因为CommandParser1()不是静态的。您需要CommandParser1的实例来调用CommandParser1()或将其定义为静态。

1。
静态方法
无法访问
非静态方法或变量。

您的代码
返回新的CommandParser1(command);
内部静态方法中
公共静态
CommandParser createParser(String命令)
,因此导致错误。

2.当您试图从
类协议实用工具
中访问
命令解析器1(命令)
这是一个
内部类
,而
这是它的外部类
您现在可以直接访问它
但是假设,当您试图从
协议实用程序类的外部访问它时,您需要创建一个外部类实例来访问这个内部类方法。1。
静态方法
无法访问
非静态方法或变量。

您的代码
返回新的CommandParser1(command);
内部静态方法中
公共静态
CommandParser createParser(String命令)
,因此导致错误。

2.当您试图从
类协议实用工具
中访问
命令解析器1(命令)
这是一个
内部类
,而
这是它的外部类
您现在可以直接访问它
但是假设,当您试图从
ProtocolUtility类的外部访问它时,您需要创建一个外部类实例来访问这个内部类方法。
公共静态命令解析器createParser(String命令)抛出异常{switch(command){
你怎么能有一个带有
字符串的开关呢?@BharatSinha:-注意,Java 7已经有一年多的历史了…
公共静态命令解析器createParser(字符串命令)抛出异常{switch(命令){
你怎么能用
字符串进行切换呢?@BharatSinha:-注意,Java 7现在已经有一年多的历史了……请不要介意使用大写和粗体的失礼。重要的是内容;评论员已经按照我的估计正确地解释了这个问题。@Mark Rottevel,如果外观对你来说比技术更重要的话回答正确,最好加入一些管理领域。请不要介意使用大写和粗体的失礼。重要的是内容;评论者已经按照我的估计正确地解释了这个问题。@Mark Rotterveel如果外观对你来说比回答的技术正确性更重要,最好加入一些管理领域劳埃德。