0

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");
}
mosawi
  • 1,283
  • 5
  • 25
  • 48
  • 2
    is `Configuration` your own class? does it have a constructor that takes one parameter? Why not create a base constructor for Toplevel and instantiate config there? – depperm Jul 22 '15 at 18:55
  • How is your `Configuration` class looks? – Razib Jul 22 '15 at 18:56
  • I very much doubt that you'd get that compiler error from that code. I suspect that `Toplevel` extends another class, which has a parameterless constructor which throws `IOException`. Please post a short but *complete* program demonstrating the problem. – Jon Skeet Jul 22 '15 at 18:59
  • 2
    Are you throwing `IOException ` in your `Configuration(String string)` overloaded constructor? If yes, you need to handle the `IOException ` while creating a new instance of `Configuration` – AnkeyNigam Jul 22 '15 at 18:59
  • Yes it's my own class, the constructor throws an I/O exception. Not sure how to throw one when I instantiate outside a method. I'll edit my original post and add the Configuration class – mosawi Jul 22 '15 at 19:01
  • That code is far longer than it needs to be, and still not complete. All we're looking for is an example which is *complete* (we can copy, paste, compile, see the error) and *short* (no more code than needed to demonstrate the problem). – Jon Skeet Jul 22 '15 at 19:11
  • I've shortened my original post – mosawi Jul 22 '15 at 19:28

2 Answers2

2

Your Configuration constructor is declared to throw an IOException. Any code that instantiates a Configuration using this constructor must catch it. If you use a variable initializer, then you can't catch it, because you can't supply a catch block; there is no block you can put here, only an expression. There is no method to declare a throws clause on either.

Your alternatives:

  • Instantiate the Configuration in a Toplevel constructor. You can catch the exception in the constructor body, or you can declare that constructor that it throws the exception.

    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.

    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. This isn't preferred, because you may have an invalid Configuration object instantiated. 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;
             }
         }
    
rgettman
  • 176,041
  • 30
  • 275
  • 357
1

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");

So you do not handle the IoException of the Configuration constructor! 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
    }
}
  • why use "this.config"? config is not a field of class Toplevel – mosawi Jul 22 '15 at 20:34
  • When you declare something in a class but not in a method then it is a field. You just didn't declared whether it is private, protected or public. In this case it is package-private. See: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – Ulme Narberau Jul 22 '15 at 20:42