[英]How to instantiate an object using it's constructor outside a method java
I'd like to instantiate an object with it's constructor outside a method. 我想用方法外的构造函数实例化一个对象。 Example: 例:
public class Toplevel {
Configuration config = new Configuration("testconfig.properties");
public void method1() {
config.getValue();
...etc
}
}
If I do this right now...I get this error.. 如果我现在就这样做...我会收到此错误。
Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor
I'd like to do something like this so I could call config anywhere in my class, right now I keep having to instantiate the Configuration objects...there's gotta be a way to do this...any help would be much appreciated, thanks in advance. 我想做这样的事情,这样我就可以在类中的任何地方调用config,现在我一直在实例化Configuration对象...必须有一种方法可以做到...任何帮助,我们将不胜感激,提前致谢。
EDIT: 编辑:
Configuration class: 配置类:
public class Configuration {
private String mainSchemaFile;
public Configuration() {
}
public Configuration( String configPath ) throws IOException {
Properties prop = new Properties();
prop.load( new FileInputStream( configPath ));
this.mainSchemaFile= prop.getProperty("MAINSCHEMA_FILE");
}
Your Configuration
constructor is declared to throw an IOException
. 您的Configuration
构造函数被声明为抛出IOException
。 Any code that instantiates a Configuration
using this constructor must catch it. 使用此构造函数实例化Configuration
任何代码都必须捕获它。 If you use a variable initializer, then you can't catch it, because you can't supply a catch
block; 如果使用变量初始化器,则无法捕获它,因为无法提供catch
块。 there is no block you can put here, only an expression. 您不能在此处放置任何块,只有一个表达式。 There is no method to declare a throws
clause on either. 在这两个方法上都没有方法声明throws
子句。
Your alternatives: 您的选择:
Instantiate the Configuration
in a Toplevel
constructor. 在Toplevel
构造函数中实例化Configuration
。 You can catch
the exception in the constructor body, or you can declare that constructor that it throws
the exception. 您可以在构造函数主体中catch
异常,也可以声明该构造函数throws
异常。
public class Toplevel { Configuration config; public Toplevel() { try { config = new Configuration("testconfig.properties"); } catch (IOException e) { // handle here } } // ...
Instantiate the Configuration
in an instance initializer in the TopLevel
class, where you can catch
the exception and handle it. 在TopLevel
类的实例初始化器中实例化Configuration
,您可以在其中catch
异常并对其进行处理。
public class Toplevel { Configuration config; { try { config = new Configuration("testconfig.properties"); } catch (IOException e) { // handle here } } // ...
Catch and handle the exception in the Configuration
constructor, so calling code doesn't have to catch the exception. 在Configuration
构造函数中捕获并处理异常,因此调用代码不必捕获异常。 This isn't preferred, because you may have an invalid Configuration
object instantiated. 这不是首选,因为您可能实例化了无效的Configuration
对象。 Calling code would still need to determine if it's valid. 调用代码仍然需要确定其是否有效。
public class Configuration { // Your instance variables private boolean isValid; public Configuration( String configPath ) { try { // Your code that might throw an IOE isValid = true; } catch (IOException e) { isValid = false; } }
When you create a new Toplevel object then you have not declared a specific constructor for it and the attribute of Toplevel is instantiated as your code describes it with Configuration config = new Configuration("testconfig.properties");
创建新的Toplevel对象时,尚未为其声明特定的构造函数,并且在代码使用Configuration config = new Configuration("testconfig.properties");
描述代码时实例化了Toplevel的属性Configuration config = new Configuration("testconfig.properties");
So you do not handle the IoException of the Configuration constructor! 因此,您不处理Configuration构造函数的IoException! A better way would be to declare a specific constructor of Toplevel like this: 更好的方法是像这样声明一个顶级的特定构造函数:
public Toplevel(){
try{
this.config = new Configuration("testconfig.properties");
} catch (IOException e) {
// handle Exception
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.