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.
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");
}
Configurationyour own class? does it have a constructor that takes one parameter? Why not create a base constructor for Toplevel and instantiate config there?Configurationclass looks?Toplevelextends another class, which has a parameterless constructor which throwsIOException. Please post a short but complete program demonstrating the problem.IOExceptionin yourConfiguration(String string)overloaded constructor? If yes, you need to handle theIOExceptionwhile creating a new instance ofConfiguration