• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Warning of "JComboBox is a raw type"

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have got two warnings for the following two lines, what's going on? eclipse tool could not fix it.
Thanks.

private JComboBox[] jcb = {new JComboBox<String>(str_gender),
"JComboBox is a raw type. References to generic type JComboBox<E> should be parameterized"


new Tst().jcb[4].addItem(coll_name);
"Type safety: The method addItem(Object) belongs to the raw type JComboBox. References to generic type JComboBox<E> should be parameterized"

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JComboBox is a generic type - it takes type parameters. You're using it with type arguments in some places, but not in other places. For example, you're creating number of JComboBox<String> objects, but when you declare the array private JComboBox[] jcb you're not specifying type parameters.

A generic type without using type parameters is called a raw type. Raw types exist in Java for backward compatibility - generics were added in Java version 5.0, and to keep Java backwards compatible with older versions it's still possible to use raw types. You should however not use raw types, that's why you get this warning.

There is, however, one more complication in your case. You cannot do private JComboBox<String>[] jcb because Java does not allow you to create an array of a parameterized type. The reason for that is complicated and has to do with how arrays and generics work in Java.

The best solution is to use a List instead of an array:

 
ye shi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your clarification, clear now:)

Jesper de Jong wrote:JComboBox is a generic type - it takes type parameters. You're using it with type arguments in some places, but not in other places. For example, you're creating number of JComboBox<String> objects, but when you declare the array private JComboBox[] jcb you're not specifying type parameters.

A generic type without using type parameters is called a raw type. Raw types exist in Java for backward compatibility - generics were added in Java version 5.0, and to keep Java backwards compatible with older versions it's still possible to use raw types. You should however not use raw types, that's why you get this warning.

There is, however, one more complication in your case. You cannot do private JComboBox<String>[] jcb because Java does not allow you to create an array of a parameterized type. The reason for that is complicated and has to do with how arrays and generics work in Java.

The best solution is to use a List instead of an array:

 
reply
    Bookmark Topic Watch Topic
  • New Topic