Hello all,I just discovered the Vector class today and am having troubleaccessing the variable elementCount. First I received a protectedpackage error so I adjusted for this by creating another class thatextends Vector..When I try to compile below, the error I'm encountering is:"MyProg.java:9: warning: [serial] serializable class myVector has nodefinition of serialVersionUIDpublic class myVector extends Vector"import java.util.*;public class MyProg1 { public static void main(String[] args) { Vector testVector = new Vector(); testVector.addElement(new String ("I am a string")); //System.out.println ( testVector.elementCount ); //protectedpackage error, cannot use }}public class myVector extends Vector{ public void myVector(){ Vector v = new Vector(); System.out.println (this.elementCount); }}Any advice is appreaciated,-t
|
|
0
|
|
|
|
Reply
|
mchew02 (18)
|
10/13/2007 1:04:59 AM |
|
Taria wrote:
> 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#synchronizedCollection(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.
--
Lew
|
|
0
|
|
|
|
Reply
|
Lew
|
10/13/2007 1:44:47 AM
|
|
Thanks Lew,That was quite informative. I'm sad and glad to hold off onjava.util.Vector class. I'll head my way into ArrayList and work outwhat I can.That encounter with Vector and the search for answers made me take anap in middle of the afternoon. :p-t
|
|
0
|
|
|
|
Reply
|
Taria
|
10/13/2007 2:32:20 AM
|
|
On Fri, 12 Oct 2007 18:04:59 -0700, Taria <mchew02@hotmail.com> wrote,quoted or indirectly quoted someone who said :>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().-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
|
|
0
|
|
|
|
Reply
|
Roedy
|
10/13/2007 6:34:22 AM
|
|
On Fri, 12 Oct 2007 18:04:59 -0700, Taria <mchew02@hotmail.com> wrote,
quoted or indirectly quoted someone who said :
>"MyProg.java:9: warning: [serial] serializable class myVector has no
>definition of serialVersionUID
see
http://mindprod.com/jgloss/compileerrormessages.html#SERIALVERISONUID
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
|
|
0
|
|
|
|
Reply
|
Roedy
|
10/13/2007 6:56:05 AM
|
|
On Fri, 12 Oct 2007 19:32:20 -0700, Taria <mchew02@hotmail.com> wrote,quoted or indirectly quoted someone who said :>That encounter with Vector and the search for answers made me take a>nap in middle of the afternoon. :pYou strike me as someone who will eventually succeed. You havecuriosity. You are respectful to those who try to help you. You arepersistent. You don't play helpless.-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
|
|
0
|
|
|
|
Reply
|
Roedy
|
10/13/2007 6:57:46 AM
|
|
On Oct 12, 8:57 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Fri, 12 Oct 2007 19:32:20 -0700, Taria <mche...@hotmail.com> wrote,
> quoted or indirectly quoted someone who said :
>
> >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.
>
> --
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
On Oct 12, 8:34 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> On Fri, 12 Oct 2007 18:04:59 -0700, Taria <mche...@hotmail.com> wrote,
> quoted or indirectly quoted someone who said :
>
> >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().
> --
> 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
|
|
0
|
|
|
|
Reply
|
Taria
|
10/14/2007 6:04:14 AM
|
|
Taria wrote:....> 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 touse a Java class than looking at it with a debugger. The APIdocumentation hides private implementation, but shows commentsexplaining the public and protected features.Patricia
|
|
0
|
|
|
|
Reply
|
Patricia
|
10/14/2007 1:27:25 PM
|
|
|
7 Replies
79 Views
(page loaded in 0.157 seconds)
|