Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem on generics from nikos blog:error generic array creation

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

why we can't able to create array at line 3?
if we modify this above code and put

or[/b],
then it is ok ,code compiles fine.

Again similar problem
why the error comes here?
here if line 3 changes to
,it works fine.
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can't instatiate an array of objects of parametrized type.
Refer to this link for explanation (Java Generics FAQ written by Angelika Langer
- look for an answer to question "Can I create an array whose component type is a type parameter?"):
http://www.angelikalanger.com/GenericsFAQ/FAQSections/TypeParameters.html#Can%20I%20create%20an%20array%20whose%20component%20type%20is%20a%20type%20parameter?
 
Simone Aiello
Greenhorn
Posts: 22
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arjun Srivastava wrote:
why the error comes here?
here if line 3 changes to
,it works fine.



This code doesn't compile because the String type is shadowed by the otherone into the <> brackets.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic