Any ideas how to avoid this code checker error?

W

Wojtek

Lew wrote :
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.

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...
 
J

John B. Matthews

Peter Duniho said:
Uh, I admit I misspelled his name. But, I simply used the same spelling
John originally posted. Why single me out?

Egad, you're right, and I've done it before! I have a clear memory of
thinking "out of respect for the esteemed name of Bloch, I won't do
_that_ again." Worse, I was relieved that it was you and not me. Hubris
and Schadenfreude. Apologies.
 
L

Lew

Wojtek said:
public class Foo
{
public static final MyObject OBJECT_SOME = new MyObject("some parm");
...
--------------------------------

What is OBJECT_SOME?

If there is a name for this, then that is what I will start calling
these constructs, otherwise...

I don't know what others call it, but I call it a "static final member", or if
not mutable, an "immutable member".

The problem with the upper-case name is that it implies but does not guarantee
immutability. If it happens that a 'MyObject' instance is mutable, then the
upper-case name is misleading at best.

In your particular code 'MyObject' instances are immutable, but that is not
discernible from the name alone. For those types that the JLS allows to be
"constant" there is no mystery. OTOH, you seem to be rigorous in ensuring
that something named with upper-case letters is immutable, so in your code I'd
have more faith. I'd have even more faith if you provided Javadocs for the
public members that asserted the immutability.
 
L

Lew

Peter said:
I dunno. I wouldn't do that for just anyone. But John's a pretty smart
guy; if I see him doing something, I might wonder if there might
actually be something to it after all. :p

That is a very measured and respectable answer. If you had added, "Provided
my ankles were secured by a bungee cord," I'd see why you were willing to
follow suit.
 
A

Arne Vajhøj

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");

The reference can not be modified like a static final String.

But the object can be modified unlike a static final String.

Arne
 
J

John B. Matthews

[QUOTE="Lew said:
I dunno. I wouldn't do that for just anyone. But John's a pretty smart
guy; if I see him doing something, I might wonder if there might
actually be something to it after all. :p

That is a very measured and respectable answer. If you had added,
"Provided my ankles were secured by a bungee cord," I'd see why you
were willing to follow suit.[/QUOTE]

Aiiieee! You guys go ahead. If needed, I'll administer first-aid until
the ambulance arrives.
 
A

Arne Vajhøj

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.

Give that the .equals to == ratio for objects probably is 100:1, then
I question the benefits from your definition.
Where I use classes as constants, I also make sure that there are no
setters, which preserves the original state of the object.

Then it is another matter.

Arne
 
W

Wojtek

Lew wrote :
I'd have even more faith if you provided Javadocs for the public members that
asserted the immutability.

In an off-the-cuff example to a news group?
 
L

Lew

Lew wrote :
In an off-the-cuff example to a news group?

Sigh.

In general. I'm speaking of general principles, here. The context of my
remarks was coding conventions, and my point was about where they do not
suffice to indicate constancy. Even in your off-the-cuff example, which I
took to be representative of general principle, pardon my error, the spelling
of 'OBJECT_SOME' doesn't suffice to aver immutability. One needs source code
access or a promise in the Javadocs to have faith in that.

I will reiterate, hopefully less confusingly. Simply spelling the name of a
non-constant variable in all upper-case letters does not, *in general*, assure
the client or maintainer of immutability. One needs additional documentation
such as Javadocs or an annotation to have more confidence that the author at
least tried for immutability. Generally speaking, that is, and as would
appear in the real world.

I should think that since you proffered your example as illustrative of
general principle that you wouldn't resort to reversing that stance in order
to get a gripe in. Silly me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top