1

Got this idea from this previous question.

How to create a generic array in Java?

Anyway, my code is like this:

public class Slice<E>
{
    private E[] data;
    public Slice(Class<E> elementType, int size)
    {
        //@SuppresWarnings({"unchecked"})
        data = (E[])Array.newInstance(elementType, size);
    }

}

I deleted the unnecessary stuff. This compiles fine when the suppress directive is commented out. When I uncomment it, I get

Error: <identifier> expected    
        data = (E[])Array.newInstance(elementType, size);
             ^

Any ideas? why would this be happening?

Community
  • 1
  • 1
Derrick
  • 2,265
  • 4
  • 31
  • 43

2 Answers2

6

You cannot put an annotation there. It must go before the public keyword. And you've mistyped the annotation name as well: change SuppresWarnings to SuppressWarnings.

EDIT: If you were using an IDE like Eclipse, you would typically use the auto-correction feature to insert the annotation. Naturally, it would be inserted in the right place and correctly spelled.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
0

Long time no java for me, but you'd put that on the method, not just inside it somewhere, right?

Noon Silk
  • 51,625
  • 6
  • 84
  • 103
  • In the other thread, they have it on the method like you're suggesting. I just tried that right now, and got this error: cannot find symbol symbol : class SuppresWarnings location: class Slice @SuppresWarnings({"unchecked"}) ^ – Derrick Aug 13 '09 at 02:29
  • Yeah, so you'll need to find the package that that class is in and import it. – Noon Silk Aug 13 '09 at 02:32
  • @silky: no ... he'll just need to spell the annotation class name correctly :-) – Stephen C Aug 13 '09 at 02:48
  • Yeah, I noticed this seconds after posting because I scrolled to see your answer :) – Noon Silk Aug 13 '09 at 02:49