Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Error when final fields (constants) not initialized at declaration
Error when final fields (constants) not initialized at declaration [message #247433] Wed, 05 September 2007 15:13 Go to next message
Eclipse UserFriend
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 #247442 is a reply to message #247433] Wed, 05 September 2007 15:24 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: merks.ca.ibm.com

This is a multi-part message in MIME format.
--------------060504070600050903050301
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

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


--------------060504070600050903050301
Content-Type: multipart/related;
boundary="------------040206070106050504020409"


--------------040206070106050504020409
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 tried your example without errors as follows (hopefully you get the
image).<br>
<blockquote><img src="cid:part1.05010204.00070008@ca.ibm.com" alt=""><br>
</blockquote>
<br>
Are you sure you haven't left something out of the example you've
copied here?
Re: Error when final fields (constants) not initialized at declaration [message #247457 is a reply to message #247433] Wed, 05 September 2007 15:30 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: wharley.bea.com

"Geri Broser" <gerold.broser@easyklick.com> wrote in message
news:fbmh1a$1fe$1@build.eclipse.org...
> 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"
> }
> }


If I add the keyword "class" before the first "MyClass", that code compiles
with no errors for me in Eclipse.

Have you tried this in a project with default compiler settings? What are
your compiler settings? What version of Eclipse are you using?
Re: Error when final fields (constants) not initialized at declaration [message #247462 is a reply to message #247442] Wed, 05 September 2007 16:15 Go to previous messageGo to next message
Eclipse UserFriend
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 16:35 Go to previous messageGo to next message
Eclipse UserFriend
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!
Re: Error when final fields (constants) not initialized at declaration (solved) [message #247472 is a reply to message #247467] Wed, 05 September 2007 17:49 Go to previous message
Eclipse UserFriend
Originally posted by: gerold.broser.easyklick.com

Thanks you very much, Ed, this helped me a lot.

Geri
Previous Topic:tools.jar
Next Topic:Language integration with existing Project Format
Goto Forum:
  


Current Time: Tue Jul 30 20:21:20 GMT 2024

Powered by FUDForum. Page generated in 0.03887 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top