[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools.jdt] Re: Error when final fields (constants) not initialized at declaration

Thanks, Ed, it was actually the "static". (Of course, silly me, statics have to be available regardless of constructor execution.)

However, what I really want to do is the following, but:

MyClass
  {
  private final static Class clazz; // msg: "The empty final field clazz is possibly not initialized"

  static
    {
    try
      {
      clazz = Class.forName( "my.package.MyClass" );
      }
    catch( ClassNotFoundException cnfe )
      {
      ...
      }
    } 	
  }

... or ...

MyClass
  {
  private final static Class clazz;

  static // no "throws ClassNotFoundException"-clause here
    {
    clazz = Class.forName( "my.package.MyClass" );
    } 	
  }

Geri


Geri,

I tried your example without errors as follows (hopefully you get the image).



Are you sure you haven't left something out of the example you've copied here? E.g., the keyword static on your constant; you can't initialize a static constant in the constructor, you'd have to do it in a static block:

public class A
{
final static String x;
static
{
x = "";
}
public A()
{
}
}






Geri Broser wrote:
Java allows the following (at least till v1.4), but JDT marks both lines with errors, without the possibility to suppress it in Preferences -> Java -> Compiler -> Errors/Warnings:

MyClass
{
private final String MY_CONSTANT; // msg: "The empty final field MY_CONSTANT is possibly not initialized"


MyClass()
{
MY_CONSTANT = "MyValue"; // msg: "The final field MyClass.MY_CONSTANT cannot be assigned"
}
}


Geri