You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lenya.apache.org by Andreas Hartmann <an...@apache.org> on 2004/08/16 15:00:02 UTC

[Java] Arrays are always mutable

Hi Lenya developers,

something I learned today:

Arrays are always mutable, so do *never* declare them final:

   public static final String[] elements = { ... };


Instead, immutable lists can be used together with a
private array for inline declaration:

   private static final Vehicle[] PRIVATE_ELEMENTS = {...};
   public static final List ELEMENTS =
     Collections.unmodifiableList(Arrays.asList(PRIVATE_ELEMENTS));


I will not invest much time to change existing code, but new
code should not contain final arrays.

-- Andreas


---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-dev-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-dev-help@cocoon.apache.org


Re: [Java] Arrays are always mutable

Posted by Andreas Hartmann <an...@apache.org>.
Rolf Kulemann wrote:

> On Mon, 2004-08-16 at 15:00, Andreas Hartmann wrote:
> 
>>Hi Lenya developers,
>>
>>something I learned today:
>>
>>Arrays are always mutable, so do *never* declare them final:
> 
> 
> Never? If I want a variable only to be assigned once, I will declare it
> as final. I use the final keyword as a contract marker, that this
> variable will never change and that this variable will always point to
> the same object.

Sure, but in case of arrays you can't ensure that this contract is
not broken. Even if you declare a public array as final, other objects
can change its contents.

[...]

> BTW: Objects are often mutable through set methods. However I declare
> variables to objects as final not to promise the state of the object
> will not change but to indicate that the variable will always point to
> the same object.

OK, I have to agree. But if you see an array just as a collection,
it is IMHO confusing that the members of the collection can be
changed ...

Anyway, findbugs complains about static array declarations :)

-- Andreas


---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-dev-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-dev-help@cocoon.apache.org


Re: [Java] Arrays are always mutable

Posted by Rolf Kulemann <ro...@apache.org>.
On Mon, 2004-08-16 at 15:00, Andreas Hartmann wrote:
> Hi Lenya developers,
> 
> something I learned today:
> 
> Arrays are always mutable, so do *never* declare them final:

Never? If I want a variable only to be assigned once, I will declare it
as final. I use the final keyword as a contract marker, that this
variable will never change and that this variable will always point to
the same object.

> 
>    public static final String[] elements = { ... };
> 
> 
> Instead, immutable lists can be used together with a
> private array for inline declaration:
> 
>    private static final Vehicle[] PRIVATE_ELEMENTS = {...};
>    public static final List ELEMENTS =
>      Collections.unmodifiableList(Arrays.asList(PRIVATE_ELEMENTS));

However, if one wants to have a read only array, this is the way I
guess.

BTW: Objects are often mutable through set methods. However I declare
variables to objects as final not to promise the state of the object
will not change but to indicate that the variable will always point to
the same object.

Makes sense?

-- 
Rolf Kulemann


---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-dev-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-dev-help@cocoon.apache.org