0

I try to create a generic array but I'm taking the error of the title.

 ByteConverter<Product> byteconverter = new ByteConverter<Product>();

 //into an inner class I have to declare a final field 
 final ByteConverter<Product>[] byteconverter2 = {byteconverter};

So, I searched at the Stackoverflow for a possible solution. I found something similar here: Cannot create an array of LinkedLists in Java...? , so I canged my code to the following:

 final ByteConverter<Product>[] byteconverter2 = {(ByteConverter<Product>[])byteconverter};

but I still take the same error. I can't understand why..Any help please?

3
  • 2
    What error are you getting? Commented Dec 12, 2012 at 15:07
  • 1
    Read through stackoverflow.com/questions/529085/…. You should find your answer. Commented Dec 12, 2012 at 15:11
  • @Rohit Jain: Cannot create a generic array of ByteConverter<Product> Commented Dec 12, 2012 at 15:11

2 Answers 2

2
final ByteConverter<Product>[] byteconverter2 = 
   new ByteConverter[]
   {
      byteconverter   
   };

this works well

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need to give type on RHS, while initializing array at the time of declaration.
and it is applicable for java 7 only
1

This compiles, though with a warning

    ByteConverter<Product> byteconverter = new ByteConverter<Product>();
    ByteConverter<Product>[] byteconverter2 = new ByteConverter[] { byteconverter };

Read here http://docs.oracle.com/javase/tutorial/java/generics/restrictions.html about restrictions for generics

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.