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