Why is size property private in ArrayList?

Hi,

I'm doing a class which will store some Events.

For this I'm extendingArrayList as I don't need Thread safe modification.

But I figured that thesize property was private whereas it is protected inVector.

Could someone tell me why?

It's a bit annoying to callsize()...

[345 byte] By [ultranestora] at [2007-10-2 9:43:33]
# 1

why?

well once you can access it directly, you could also change it ala create a vector with 2 elements but size = 3 or some such. That would be bad

as far as making a call to size(), optimizations like that are almost always marginal and often take more time and energy in coding and debugging than what you gain

tjacobs01a at 2007-7-16 23:49:15 > top of Java-index,Core,Core APIs...
# 2
I see, that makes sense in term of API use consistency...Thanks.Let's copy size() locally in the loops where I need it... ;)
ultranestora at 2007-7-16 23:49:15 > top of Java-index,Core,Core APIs...
# 3

>For this I'm extending ArrayList as I don't need Thread safe modification.

Bad idea.

Instead I would say you want to encapsulate a List, rather than extend a List.

ie

public class MyEvents{

private List events;

public void addEvent(Event e){

events.add(e);

}

...

}

That way you only provide the methods that you WANT people to be able to access. Also you are not tied into array list, and can change it if need be.

Of course this is all just IMNSHO.

Cheers,

evnafets

evnafetsa at 2007-7-16 23:49:15 > top of Java-index,Core,Core APIs...
# 4

> >For this I'm extending ArrayList as I don't need

> Thread safe modification.

> Bad idea.

> Instead I would say you want to encapsulate a List,

> rather than extend a List.

>

> ie

> public class MyEvents{

>private List events;

>

>public void addEvent(Event e){

>events.add(e);

>}

>...

> }

>

> That way you only provide the methods that you WANT

> people to be able to access. Also you are not tied

> into array list, and can change it if need be.

>

> Of course this is all just IMNSHO.

> Cheers,

> evnafets

Unless it really is just a list of Events, in which case he should switch to 5.0.

Adeodatusa at 2007-7-16 23:49:15 > top of Java-index,Core,Core APIs...
# 5

> But I figured that the size property was

> private whereas it is protected in Vector.

> Could someone tell me why?

> It's a bit annoying to call size()...

I almost always use getters and setters to access and change variables, even from within the class. Doing so allows me to change implementation without changing much code. (For example, say you have a class full of boolean values. Well if you wanted to change the implementation, say, to store the boolean values as bit values to save space, all you would have to change would be the getter and setter methods, no other code. The same holds true if you want to add checking to your setter methods.)

- elAmericano

theAmericana at 2007-7-16 23:49:15 > top of Java-index,Core,Core APIs...
# 6

In my experience 99% of all user created 'collection' classes that are derived from concrete java collection classes, are incorrectly written, as stated earlier. If you find yourself wanted to do this, i would at least sit up and say to myself, dang it, this is probably not what i want to do.

dmbdmba at 2007-7-16 23:49:15 > top of Java-index,Core,Core APIs...
# 7
In my experience 99% of all user created 'collection' classes that are derived from concrete java collection classes, are incorrectly writtenIn fact plenty of JRE classes can be accused of inaprropriate extension anyway. Stack and Properties being prime examples.
itchyscratchya at 2007-7-16 23:49:15 > top of Java-index,Core,Core APIs...