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?

asked Dec 12, 2012 at 15:06
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

answered Dec 12, 2012 at 15:07
2
  • You don't need to give type on RHS, while initializing array at the time of declaration. Commented Dec 12, 2012 at 15:08
  • and it is applicable for java 7 only Commented Dec 12, 2012 at 15:09
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

answered Dec 12, 2012 at 15:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.