> Hello all,
>
> I just discovered the Vector class today and am having trouble
Do you mean java.util.Vector? If so, you'll be much, much better off starting
with ArrayList as your first List implementor. Please hold off on Vector, a
very old, pre-Collection class, until you know about multi-threaded programming.
> accessing the variable elementCount. First I received a protected
> package error so I adjusted for this by creating another class that
> extends Vector..
This is an antipattern.
> When I try to compile below, the error I'm encountering is:
> "MyProg.java:9: warning: [serial] serializable class myVector has no
> definition of serialVersionUID
> public class myVector extends Vector"
This has to do with building a correct implementation of java.io.Serializable.
Let's defer that for a moment. It is nothing to do with access to the
protected variable, something else you should not do.
You will be much, much better off either extending (yecch) ArrayList, or
extending AbstractList, or building your own version of a List altogether.
List provides the method size() that you should use in preference to any
direct access to a member variable.
However, you should not extend List, you should include a List as a member
variable of your custom class.
Besides being way too old, Vector has the feature that all its methods are
synchronized. Usually this is overhead you do not want, because you only need
thread-local access to your List. Even if you do need to synchronize access,
you can use one of the prebuilt concurrent structures, or make a
Collections.synchronizedCollection( yourList ).
<http://java.sun.com/javase/6/docs/api/java/util/Collections.html#synchronizedCol
lection(java.util.Collection)>
Now, about that java.io.Serializable implementation.
<http://java.sun.com/javase/6/docs/api/java/io/Serializable.html>
Read and study this link, and the related material in Joshua Bloch's book,
/Effective Java/.
If you make a class implement java.io.Serializable you pretty much should
have, but the language does not require, a member variable:
ANY-ACCESS-MODIFIER static final long serialVersionUID
where "ANY-ACCESS-MODIFIER" can be any access modifier, including no access
modifier (package-private). That's what the error message told you was missing.
Implementing java.io.Serializable is a huge responsibility. It commits the
class maintainer permanently to details of an implementation, and to an
additional exposed face to the class that provides a back door to the internals.

Signature
Lew
Taria - 13 Oct 2007 03:32 GMT
Thanks Lew,
That was quite informative. I'm sad and glad to hold off on
java.util.Vector class. I'll head my way into ArrayList and work out
what I can.
That encounter with Vector and the search for answers made me take a
nap in middle of the afternoon. :p
-t
Roedy Green - 13 Oct 2007 07:57 GMT
>That encounter with Vector and the search for answers made me take a
>nap in middle of the afternoon. :p
You strike me as someone who will eventually succeed. You have
curiosity. You are respectful to those who try to help you. You are
persistent. You don't play helpless.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Taria - 14 Oct 2007 07:04 GMT
On Oct 12, 8:57 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> >That encounter with Vector and the search for answers made me take a
> >nap in middle of the afternoon. :p
[quoted text clipped - 6 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
On Oct 12, 8:34 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> >I just discovered the Vector class today and am having trouble
> >accessing the variable elementCount. First I received a protected
[quoted text clipped - 7 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
lol oh! Now I don't feel too smart. :p My debugger showed me the
structure of the Vector class and I assumed immediately that was the
name of the field.
Thank you for your kind words, Roeddy, that's encouraging, indeed.
I'm persistant because I get the greatest satisfaction when I
understand a little more esp on the objects concepts of Java, objects
has terrorized me for a long time!
I'm done with the ArrayList/objects version of the Quicksort search
for the recursion assignment. :))) I really only needed one program
done with or without objects but I wanted to try both versions and
because of that...I FINALLY understand how to write and access a
class. whee! haha i know this part is a walk in a park for you but
it's monumental for me.
Anyway..thank you again, Lew and Roeddy (to name a few), both of you
have been very informative and I feel lucky to have found you
guys. :) (I've seen posts from you guys on other boards, too. lol
you guys are everywhere)
-t
Patricia Shanahan - 14 Oct 2007 14:27 GMT
...
> lol oh! Now I don't feel too smart. :p My debugger showed me the
> structure of the Vector class and I assumed immediately that was the
> name of the field.
...
Reading the API documentation is a MUCH better way of finding out how to
use a Java class than looking at it with a debugger. The API
documentation hides private implementation, but shows comments
explaining the public and protected features.
Patricia
>I just discovered the Vector class today and am having trouble
>accessing the variable elementCount. First I received a protected
>package error so I adjusted for this by creating another class that
>extends Vector..
Vector has been largely replaced by ArrayList.
You don't need to get at elementCount. You can use Vector.size().

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
>"MyProg.java:9: warning: [serial] serializable class myVector has no
>definition of serialVersionUID
see
http://mindprod.com/jgloss/compileerrormessages.html#SERIALVERISONUID

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com