If I have a class that will have some data elements that are arrays,
should I create the array in the constructor or out?
class Test {
public String[] var = new String[123];
...
or
class Test {
public String[] var;
public Test() {
var = new String[123];
...
Why and what difference would it make?
Thanks,

Signature
Knute Johnson
email s/nospam/knute/
Chris Smith - 02 Feb 2006 23:04 GMT
> If I have a class that will have some data elements that are arrays,
> should I create the array in the constructor or out?
[quoted text clipped - 13 lines]
>
> Why and what difference would it make?
If you're asking the question, I would say it's clearer to initialize
the field at its declaration. What difference?
1. If you keep the field and its initial value in one place, that's one
fewer place in the code where you've scattered knowledge about the
representation of the data. While it's certainly far MORE important to
reduce coupling between classes than within a class, the latter is still
not completely unimportant.
2. The initialization can provide information that helps a reader
comprehend the purpose or usage of the variable. That information is
best kept in one place.
3. In common usage, initializing something in the constructor tends to
imply that it involve significant work, or that the initialization is
specific to one means (versus others) of creating the object. That's a
misleading implication in this case.
This is all about readability and marginal code improvements... but it's
the collection of many such things that ultimately makes the difference
in quality of code.
As for why you'd choose to initialize something in a constructor, you
might do so if the initialization is somewhat involved, might fail, or
depends on the way the object is being created. Such things are much
clearer when done in a constructor (and the last depends on some
initialization in at least one constructor to work in the first place).
HTH,

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Daniel Dyer - 02 Feb 2006 23:07 GMT
> If I have a class that will have some data elements that are arrays,
> should I create the array in the constructor or out?
[quoted text clipped - 15 lines]
>
> Thanks,
I'd go with the first one. If you then add another constructor you don't
have to repeat yourself or delegate to the default constructor. Also, if
the field will only be assigned on object creation, I'd make it final.
I'd also argue that it's more readable to assign it at the same place you
declare the field.
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
Knute Johnson - 03 Feb 2006 03:36 GMT
> If I have a class that will have some data elements that are arrays,
> should I create the array in the constructor or out?
[quoted text clipped - 15 lines]
>
> Thanks,
Thanks for the replies.

Signature
Knute Johnson
email s/nospam/knute/
Ranganath Kini - 03 Feb 2006 15:48 GMT
Well the choice may also depend on the fact that if u are certain of
the array size when the object is instantiated.
In other words, if your array size is constant at 123 then it makes
sense to use the first technique.
But if you expect that the size of the array can be specified when
instantiating the class, then the second technique wud be useful.
Example:
class Test {
public String[] var;
public Test( int size ) {
var = new String[ size ];
// rest of ur constructor implementation
}
// rest of ur class implementation
}
Since the size of the array is specified when constructing the Test
class, the second technique is more useful.
Hope it helps!
Knute Johnson - 03 Feb 2006 18:14 GMT
> Well the choice may also depend on the fact that if u are certain of
> the array size when the object is instantiated.
[quoted text clipped - 21 lines]
>
> Hope it helps!
Thanks Ran.

Signature
Knute Johnson
email s/nospam/knute/