On Sun, 14 Feb 1999, alexander lang wrote:

> Hi all.
> 
> Does anyone know if one is allowed to initialize static final variables 
> within the "try" clause of a static initializer block?
> 
> (eg.
>       static final int x;
>       static{
>                 try
>           {
>              x = 5;
>           }
>       catch(exception e)
>          {...and so on ..}
> )
> 
> I am working on a software project which has code like this. I am 
> getting compile - time error messages which state that I am NOT allowed 
> to fo this.   Are there some tricks one can use to "trick" the compiler.  
> I am assuming that the code previously worked(?).  I am using 
> jdk1.1.5-glibc.
> 
> Any assistance would be high ly appreciated.
> 
> Thank YOU!
> 
> ______________________________________________________
> Get Your Private, Free Email at http://www.hotmail.com
> 


Hello,

I do not think that would be allowed. I just tried to compile this class
with the JDK 1.2 compiler and the jikes compiler and they do not accept it.

public class TryInStaticInit {

    static final boolean cond;

    static {
        try
        {
            cond = true;
        }
        catch(Exception e) {
            // do nothing
        }
    }
}




with javac from JDK 1.2


TryInStaticInit.java:3: Blank final variable 'cond' may not have been initialized. It 
must be assigned a value in an initializer, or in every constructor.
    static final boolean cond;
                             ^




with jikes


Found 1 semantic error and issued 2 warnings compiling "TryInStaticInit.java":

     3.     static final boolean cond;
                                 <-->
*** Error[116]: A blank class final variable must be initialized in a static 
initializer block. It is assumed to be initialized


    10.         catch(Exception e) {
                      <--------->
*** Caution[ 186]: This catch block may be unreachable because there is no exception 
whose type is assignable to "java/lang/Exception" that can be thrown during execution 
of the body of the try block




I hope that helps
Mo DeJong
dejong at cs.umn.edu

Reply via email to