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");
}
7
  • 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? Commented Jul 22, 2015 at 18:55
  • How is your Configuration class looks? Commented Jul 22, 2015 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. Commented Jul 22, 2015 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 Commented Jul 22, 2015 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 Commented Jul 22, 2015 at 19:01

2 Answers 2

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;
             }
         }
    
Sign up to request clarification or add additional context in comments.

5 Comments

Can you give me an example of this?
@mosawi I've added examples.
I'm going to try this. I'll report back
Looks like you have a Configuration constructor inside toplevel? Do you mean a toplevel constructor?
I'm still getting the same problem. I don't have "access" to just one instance of config in my top level class...
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
    }
}

2 Comments

why use "this.config"? config is not a field of class Toplevel
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: docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.