[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

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

JPEG image