views:

160

answers:

2

Got this idea from this previous question.

http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation

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?

A: 

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

Noon Silk
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 symbolsymbol : class SuppresWarningslocation: class Slice<E> @SuppresWarnings({"unchecked"}) ^
Derrick
Yeah, so you'll need to find the package that that class is in and import it.
Noon Silk
@silky: no ... he'll just need to spell the annotation class name correctly :-)
Stephen C
Yeah, I noticed this seconds after posting because I scrolled to see your answer :)
Noon Silk
+3  A: 

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
Just saw that in my comment right after I posted it. Thanks :)
Derrick