It's not a secret anymore!
The moose likes Java in General and the fly likes Vector Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Vector Question" Watch "Vector Question" New topic

Vector Question


Joined: Apr 08, 2001
Posts: 8
When you subclass a vector can you put primitives in it?
(I know vectors only hold objects)
public class Subvector extends Vector
int i= 5; //does error occur here?
public static void main(String args[ ]){
Subvector sv = new Subvector( ) //does error occur here?
System.out.println(sv.i) //does error occur here?
Thanks in Advance!
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
You might want to adjust your username to comply with the
naming policy here. (They mention it when you sign up)
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
All you have done is create a Vector that can hold one and only one int. Not a whole array of ints.
Clearly, based on the questions you have asked you need to go to the bunkhouse and buy a simple book on Java and start at the beginning. You seem completely lost.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Vectors can only hold objects. If you need to put a primitive in a Vector you need to wrap it in a Wrapper class.
Integer j = 5; //puts the int value in an Object wrapper.
Subvector v = new Subvector();
In your example your i value is not "in" the Vector in the sense that the other elements are in it. When you load a Vector you load the elements to the "location" that the methods look to for working the Vector. For instance you can not use the sv.get(0) method of Vector to get your int value because it is outside of the location defined in the parent class. You can HAVE the int variable - it is just extra stuff.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
All of your "does an error occur here" questions can be answered by javac. Why not simply run the compiler rather than go to all the trouble to post to a newsgroup?

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
I agree. Here's the link:
subject: Vector Question
It's not a secret anymore!