• 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.
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!