I have a rather simple question for you all :-D
Im writing a class that involves the declaration of two static final arrays
as class variables.
I initilize those static arrays in my static{} method.
public class MyClass
{
static final int ARRAY_LENGTH = 4; // The length of both ARRAY1 and ARRAY2.
static final char[] ARRAY1;
static final char[] ARRAY2;
static
{
// Construct the arrays:
ARRAY1 = new char[MyClass.ARRAY_LENGTH];
ARRAY2 = new char[MyClass.ARRAY_LENGTH];
// Fill the arrays:
Arrays.fill(MyClass.ARRAY1, '0');
Arrays.fill(MyClass.ARRAY2, '9');
}
}
Now, Im wondering, would it be more proper to use the Arrays.fill method to
fill ARRAY1, then just not filling ARRAY1 (since char[] are filled with '0'
on instantiation), or am I just wasting processessor cycles/heap space?
Thanks in advance! :-)
Ryan Stewart - 18 Mar 2004 13:42 GMT
> I have a rather simple question for you all :-D
>
[quoted text clipped - 28 lines]
>
> Thanks in advance! :-)
I think you better check that one out. To my knowledge, char is not
initialized to '0', but to 0 by default. I will check...
Looking in the JLS 1.0, I see:
"For type char, the default value is the null character, that is, '\u0000'."
Tony Morris - 19 Mar 2004 10:50 GMT
> I have a rather simple question for you all :-D
>
> Im writing a class that involves the declaration of two static final arrays
> as class variables.
>
> I initilize those static arrays in my static{} method.
No, you initialize the static arrays in a static *initializer*, not a static
method.
> Arrays.fill(MyClass.ARRAY1, '0');
If you hadn't executed this statement, the array values values would be
initialized to 0.
But since you did execute this statement, the array values have been
initialized to '0' (which when converted to type int, is 48).
Side note: declaring a non-private reference type data member that is
mutable (e.g. an array) is bad practice (regardless of whether the reference
is declared final).

Signature
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
Home : +61 7 5502 7987
Work : +61 7 5552 4076
Mobile : 0408 711 099
(2003 VTR1000F)
Roedy Green - 19 Mar 2004 21:41 GMT
>static final char[] ARRAY1;
Is that kosher? Do static final arrays ALSO get all caps or only
scalars?
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Eric Sosman - 19 Mar 2004 22:23 GMT
> >static final char[] ARRAY1;
>
> Is that kosher? Do static final arrays ALSO get all caps or only
> scalars?
Looks kosher to me. For precedent, consider
Collections.EMPTY_SET
TextAttribute.BACKGROUND
Locale.CANADA_FRENCH
... and so on, none of which are scalars.

Signature
Eric.Sosman@sun.com
Tony Morris - 19 Mar 2004 23:16 GMT
> Is that kosher? Do static final arrays ALSO get all caps or only
> scalars?
Yes. The reference type does not determine whether or not the caps notation
of naming is used, but the fact that it is final and a class member.
Java Code Conventions Section 9:
"The names of variables declared class constants and of ANSI constants
should be all uppercase with words separated by underscores ("_"). (ANSI
constants should be avoided, for ease of debugging.) "
This is not to be confused with the fact that the reference is declared
final, but the array itself is mutable (as all arrays are) and non-private,
therefore, providing direct access to altering the object's state (which we
all know is A Bad Thing[TM]).
What is not "kosher" is the use of terms, such as "scalar" :)
The JLS explicitly refers to scalars [sic] as "primitive types".

Signature
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Roedy Green - 19 Mar 2004 21:44 GMT
>Now, Im wondering, would it be more proper to use the Arrays.fill method to
>fill ARRAY1, then just not filling ARRAY1 (since char[] are filled with '0'
>on instantiation), or am I just wasting processessor cycles/heap space?
You need the explicit fill since arrays start out with binary 0 0x00
in them not the character '0' which is 0x30.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.