Error when final fields (constants) not initialized at declaration [message #247433] |
Wed, 05 September 2007 11:13  |
Eclipse User |
|
|
|
Originally posted by: gerold.broser.easyklick.com
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
|
|
|
|
|
Re: Error when final fields (constants) not initialized at declaration [message #247462 is a reply to message #247442] |
Wed, 05 September 2007 12:15   |
Eclipse User |
|
|
|
Originally posted by: gerold.broser.easyklick.com
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
>
|
|
|
Re: Error when final fields (constants) not initialized at declaration [message #247467 is a reply to message #247462] |
Wed, 05 September 2007 12:35   |
Eclipse User |
|
|
|
Originally posted by: merks.ca.ibm.com
This is a multi-part message in MIME format.
--------------000607020301090905040900
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit
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
>>
--------------000607020301090905040900
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Geri,<br>
<br>
I thought so!
|
|
|
|
Powered by
FUDForum. Page generated in 0.05961 seconds