Geri,
I thought so! :-)
You can use this type of approach:
class MyClass
{
private final static Class CLASS;
static
{
Class clazz = null;
try
{
clazz = Class.forName( "my.package.MyClass" );
}
catch( ClassNotFoundException cnfe )
{
}
CLASS = clazz;
}
}
I.e., compute the value and assign it once you've decided what the
final value should be.
Geri Broser wrote:
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:
class 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 ...
class MyClass
{
private final static Class clazz;
static // no "throws ClassNotFoundException"-clause here
{
clazz = Class.forName( "my.package.MyClass" );
}
}
Geri
Ed Merks wrote:
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
|