Re: Any ideas how to avoid this code checker error?
Lew wrote :
Wojtek wrote:
I am not confused at all. If it is static and final, then it is a class
constant. The fact that it has attributes which can be modified (if
exposed) is a moot point. The _reference_ cannot be changed, and so it is a
constant.
That is not the definition of a class constant! The JLS defines the term.
It is an extremely important distinction; initialization of class constants
and their storage differs from other 'static final' variables.
You are not using the correct definition.
<http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#10931>
We call a variable, of primitive type or type String, that is final and
initialized with a compile-time constant expression (?15.28) a constant
variable. Whether a variable is a constant variable or not may have
implications with respect to class initialization (?12.4.1), binary
compatibility (?13.1, ?13.4.9) and definite assignment (?16).
So it is limited to primitives or String. Ok, then what would you call
this? Like String it is barred from being modified after
initialisation.
--------------------------------
public class Foo
{
public static final MyObject OBJECT_SOME = new MyObject("some parm");
public static final MyObject OBJECT_TWO = new MyObject("two parm");
public class MyObject()
{
private String parm;
private MyObject(String parm )
{
this.parm = parm;
}
public String getParm()
{
return this.parm;
}
}
}
....
System.out.println(Foo.OBJECT_SOME.getParm());
--------------------------------
What is OBJECT_SOME?
If there is a name for this, then that is what I will start calling
these constructs, otherwise...
--
Wojtek :-)