1

I have a problem which reduces down to considering this class:

class myClass
{
    static int s_int = getInteger();

    static int getInteger() throws myClassException
    {
        ...

Here's my problem: this will not compile since getInteger() throws myClassException and I don't have a try catch block when initialising s_int.

One solution, of course, would be to build a getIntegerAndDealWithTheException() which doesn't throw an exception and call that instead when initialising s_int. But I'd rather not as that is not so pretty: I'd rather not litter the code with stubs.

Am I missing a syntatic trick here with my initialisation of s_int?

Many thanks!

Bathsheba
  • 231,907
  • 34
  • 361
  • 483

5 Answers5

2

You could use a static initializer. A static initializer is a block of code in between static { and } used to initialize static variables that take more code than a simple declaration and expression.

class myClass
{
    static int s_int;

    static
    {
       try {
          s_int = getInteger();
       } catch (myClassException e) {
          // Handle it here.
       }
    }

    static getInteger() throws myClassException
    {
        ...

Static initializers may not throw checked Exceptions, according to JLS 11.2.3. To quote:

It is a compile-time error if a class variable initializer (§8.3.2) or static initializer (§8.7) of a named class or interface can throw a checked exception class.

So you must catch the Exception, meaning that you need more than a simple declaration and expression, so a static initializer does the job here.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

You can initialize static attributes inside the class static block.

In your case:

class myClass
{
    static int s_int;

    static {
        try {
            s_int = getInteger();
        } catch (myClassException e) {
            // exception handling code
        }
    }

    static getInteger() throws myClassException
    {
        ...
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
  • As you can see there is a big competition between the people answering questions on SO, a constant fight with the time, that is why I first provide a short answer and then extend it :) – Adam Siemion May 22 '13 at 21:47
1

I will elaborate a little on what to do when you catch the exception.

The class initialization should be put inside a static initializer. As your compiler warns, you can't leave an uncaught exception inside a static initializer. You must catch it and do something with it. If you can't recover from the exception then your class is not initialized. Then you must throw an ExceptionInInitializerError that signals that this classes state is invalid.

class MyClass {
    static int s_int;

static {
    try {
        s_int = getInteger();
    catch (MyClassException e) {
        throw new ExceptionInInitializerError(e);
        // or if it is ok, s_it = some_default_value; 
    }

static int getInteger() throws MyClassException {
   ...

Here you can find a more detailed explanation.

Community
  • 1
  • 1
nakosspy
  • 3,904
  • 1
  • 26
  • 31
0

You could (but probably shouldn't) use a static block:

class myClass {

    static int s_int;
    static {
        try {
           s_int = getInteger();
        }
        catch(Exception e) {
            // ...
        }
    }

}

The alternative is to lazy-load the value.

class myClass {

    static Integer s_int = null;

    public static int getInteger() throws Exception {
        if(s_int == null) {
            s_int = /* ? */
        }
        return s_int;
    }

    public static void wtv() {
        // never refer to the static member - use the method,
        // which will lazy-load the value
        int s_int = getInteger();
    }

    public static void doSomething() {
        // never refer to the static member - use the method,
        // which will lazy-load the value
        int s_int = getInteger();
    }

}

... and then always refer to getInteger(), never directly to the static member.

Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
-1

You should consider reading the chapter "Use unchecked expetions" of the book "Clean Code" from Robert C. Martin. After that I do not think you will have this kind of problem.

Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31