Two Laptop Bag*
The moose likes Java in General and the fly likes Generics related warning in Vector add method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics related warning in Vector add method" Watch "Generics related warning in Vector add method" New topic
Author

Generics related warning in Vector add method

Leena Diwan
Ranch Hand

Joined: Jun 18, 2001
Posts: 351
Hi

I am creating a Vector instance.
And adding elements like vectorInstance.add("myData");

To the add method I am getting warning in my IDE (Eclipse) as following-
Type safety: The method add(Object) belongs to the raw type Vector. References to generic type Vector<E> should be parameterized

I am using jdk1.6. All I know is this is generics related warning.
What would be a good programming practice in this case?

Can you give me example? And where to check in case I get such warnings for any other classes in java?

Thank you!

Regards,
Leena


[SCJP2, SCWCD1.3, SCBCD]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Lesson: Generics

In your case, declare and initialize your vector as this:
As a side note, don't declare vectorInstance as a Vector, but as a List:
This is called programming against interfaces; do a search around this forum to find out why this is better (the question has been asked many, many times).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Another point: Is there a specific reason that you are using class Vector instead of class ArrayList?

Class Vector is one of the old legacy collection classes from Java 1.0 / 1.1. In Java 1.2 (long ago...) a new collections framework was introduced, including ArrayList to replace Vector (there are small differences, but those are not relevant for most situations). Prefer the newer classes such as ArrayList above the old ones such as Vector.

Also see the Collections trail in Sun's Java Tutorials.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Generics related warning in Vector add method
 
Similar Threads
Generics
Generics <?>
generics
Generics Generics Generics !!!
Generics