Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Warning of "JComboBox is a raw type"  RSS feed

 
ye shi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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"

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!