Java-Can';t在静态类中声明对象

Java-Can';t在静态类中声明对象,java,singleton,static-methods,Java,Singleton,Static Methods,我试图为一个系统类实现一个单例模式。我发现的示例不可编译(例如)。在非静态类中有一个静态方法。因此,我将该类设置为静态,在我尝试为Timer类创建成员变量之前,一切都很好 现在我收到消息“没有可访问的scene_3d类型的封闭实例。必须使用封闭实例限定分配…” 我已经搜索过了,但是没有人为我编译单例模式。顺便说一下,我正在使用Processing(一个Java IDE/扩展)。任何关于如何解决这个问题的想法都会非常有帮助。谢谢 static public class DemoSystem {

我试图为一个系统类实现一个单例模式。我发现的示例不可编译(例如)。在非静态类中有一个静态方法。因此,我将该类设置为静态,在我尝试为Timer类创建成员变量之前,一切都很好

现在我收到消息“没有可访问的scene_3d类型的封闭实例。必须使用封闭实例限定分配…”

我已经搜索过了,但是没有人为我编译单例模式。顺便说一下,我正在使用Processing(一个Java IDE/扩展)。任何关于如何解决这个问题的想法都会非常有帮助。谢谢

static public class DemoSystem {
  private static DemoSystem instance = null;  
  protected DemoSystem() {}

  public static DemoSystem Inst() {
    if( instance == null ) {
      instance = new DemoSystem();
    }
    return instance;
  }

  void init() {
    Timer timer = new Timer();  
  }

  int getTime() {
    return timer.elapsedTime;
  }
}

计时器在init内声明。它必须在类中启动,以便get方法可以访问它,例如:

static public class DemoSystem {
  private static DemoSystem instance = null;
  private Timer timer;
  protected DemoSystem() {}

  public static DemoSystem Inst() {
    if( instance == null ) {
      instance = new DemoSystem();
    }
    return instance;
  }

  public void init() {
    timer = new Timer();  
  }

  public int getTime() {
    return timer.elapsedTime;
  }
}

您还缺少这两个方法上的公共限定符。

计时器在init中声明。它必须在类中打开,以便get方法可以访问它,例如:

static public class DemoSystem {
  private static DemoSystem instance = null;
  private Timer timer;
  protected DemoSystem() {}

  public static DemoSystem Inst() {
    if( instance == null ) {
      instance = new DemoSystem();
    }
    return instance;
  }

  public void init() {
    timer = new Timer();  
  }

  public int getTime() {
    return timer.elapsedTime;
  }
}

您还缺少这两个方法的公共限定符。

我认为您不能将整个类声明为静态:

static public class DemoSystem {
应该是:

public class DemoSystem {
而且,没有100%可能有语法错误(缺少分号…):

应该是

   protected DemoSystem() {};

我认为您不能将整个类声明为静态:

static public class DemoSystem {
应该是:

public class DemoSystem {
而且,没有100%可能有语法错误(缺少分号…):

应该是

   protected DemoSystem() {};

标准的单例模式是有一个
private
构造函数和一个静态实例变量,因此:

public class DemoSystem {
  private static DemoSystem instance = null;
  private Timer timer;
  protected DemoSystem() {}

  public static DemoSystem Inst() {
    if( instance == null ) {
      instance = new DemoSystem();
    }
    return instance;
  }

  void init() {
    timer = new Timer();  
  }

  int getTime() {
    return timer.elapsedTime;
  }
}

一旦修复了导入,这应该可以正常工作。

标准的单例模式是有一个
private
构造函数和一个静态实例变量,因此:

public class DemoSystem {
  private static DemoSystem instance = null;
  private Timer timer;
  protected DemoSystem() {}

  public static DemoSystem Inst() {
    if( instance == null ) {
      instance = new DemoSystem();
    }
    return instance;
  }

  void init() {
    timer = new Timer();  
  }

  int getTime() {
    return timer.elapsedTime;
  }
}

一旦您修复了导入,这应该可以正常工作。

首先,我认为顶级类不可能是静态的,因为最初没有嵌套类,也不能向任何类添加静态

所以下面的内容是无效的

static public class DemoSystem {

 ...
}
你要改变的就是喜欢

public class DemoSystem {
  private static DemoSystem instance = null;
  private Timer timer;
  protected DemoSystem() {}

  public static DemoSystem Inst() {
    if( instance == null ) {
      instance = new DemoSystem();
    }
    return instance;
  }

  void init() {
    timer = new Timer();  
  }

  int getTime() {
    return timer.elapsedTime;
  }
 }
最好对单例使用
Enum

“此方法在功能上等同于公共字段方法,只是它更简洁,免费提供序列化机制,并提供铁一般的保证,即使面对复杂的序列化或反射攻击,也可以防止多次实例化。虽然这种方法尚未被广泛采用,单元素枚举类型是实现单元素枚举的最佳方式。

因此,使用
Enum
您的类看起来像:

public enum DemoSystem {
    INSTANCE;
    private Timer timer;
    void init() {
       timer = new Timer();  
     }

    int getTime() {
       return timer.elapsedTime;
    }
}

首先,我认为顶级类不可能是静态的,最初没有嵌套类,也不能向任何类添加静态

所以下面的内容是无效的

static public class DemoSystem {

 ...
}
你要改变的就是喜欢

public class DemoSystem {
  private static DemoSystem instance = null;
  private Timer timer;
  protected DemoSystem() {}

  public static DemoSystem Inst() {
    if( instance == null ) {
      instance = new DemoSystem();
    }
    return instance;
  }

  void init() {
    timer = new Timer();  
  }

  int getTime() {
    return timer.elapsedTime;
  }
 }
最好对单例使用
Enum

“此方法在功能上等同于公共字段方法,只是它更简洁,免费提供序列化机制,并提供铁一般的保证,即使面对复杂的序列化或反射攻击,也可以防止多次实例化。虽然这种方法尚未被广泛采用,单元素枚举类型是实现单元素枚举的最佳方式。

因此,使用
Enum
您的类看起来像:

public enum DemoSystem {
    INSTANCE;
    private Timer timer;
    void init() {
       timer = new Timer();  
     }

    int getTime() {
       return timer.elapsedTime;
    }
}

有几点与发布的源代码片段有关

  • 首先,顶级类不能声明为静态类

  • 您创建的public方法(publicstaticdemosystem Inst())不能保证它只在并发的情况下创建单例对象。 为此,仅在声明时初始化对象,如下所示: 私有静态DemoSystem实例=新建DemoSystem()

  • 或者正确地同步该方法

  • 使Timer成为类级别变量

  • 什么是timer.elapsedTime。我认为timer类中没有任何这样的属性。 如果您使用的是不同的api,请进行必要的导入

  • 最后的代码如下所示:

    import java.util.Timer;
    
    public class DemoSystem {
        // Declared the variable at class level.
        Timer timer = null;
    
        // Initializing the object here only.
        private static DemoSystem instance = new DemoSystem();
    
        // Made the constructer private.
        private DemoSystem() {
        }
    
        // Static method to get singleton instance
        public static DemoSystem Inst() {
            return instance;
        }
    
        void init() {
            timer = new Timer();
        }
    
        int getTime() {
            //commented the statement so to compile the class  
            //return timer.elapsedTime;
            return 1;
        }
    }
    

    有几点与发布的源代码片段有关

  • 首先,顶级类不能声明为静态类

  • 您创建的public方法(publicstaticdemosystem Inst())不能保证它只在并发的情况下创建单例对象。 为此,仅在声明时初始化对象,如下所示: 私有静态DemoSystem实例=新建DemoSystem()

  • 或者正确地同步该方法

  • 使Timer成为类级别变量

  • 什么是timer.elapsedTime。我认为timer类中没有任何这样的属性。 如果您使用的是不同的api,请进行必要的导入

  • 最后的代码如下所示:

    import java.util.Timer;
    
    public class DemoSystem {
        // Declared the variable at class level.
        Timer timer = null;
    
        // Initializing the object here only.
        private static DemoSystem instance = new DemoSystem();
    
        // Made the constructer private.
        private DemoSystem() {
        }
    
        // Static method to get singleton instance
        public static DemoSystem Inst() {
            return instance;
        }
    
        void init() {
            timer = new Timer();
        }
    
        int getTime() {
            //commented the statement so to compile the class  
            //return timer.elapsedTime;
            return 1;
        }
    }
    

    可能重复的可能重复编译失败。我得到错误“字段实例不能声明为静态;静态字段只能在静态或顶级类型中声明。“您认为这是处理的限制吗?顺便说一下,我正在使用自己的计时器类。您好,我认为这可能是处理的一个限制,因为我已经在EclipseJuno中成功编译了相同的代码。您现在可以编译并运行您的代码了。如果没有,请告诉你开发时使用的IDE和处理插件。这无法编译。我得到错误“字段实例不能声明为静态;静态字段只能声明为静态或顶级类型”。您认为这是处理的限制吗?顺便说一下,我正在使用自己的计时器类。您好,我认为这可能是处理的一个限制,因为我已经在EclipseJuno中成功编译了相同的代码。您现在可以编译并运行您的代码了。如果没有,请告诉你的开发使用哪个IDE和处理插件。