Note that there are some explanatory texts on larger screens.

plurals
  1. POJava annotations: at variable initialization, but not assignment?
    text
    copied!<p>I've been having trouble understanding where exactly an annotation has to or can be placed.</p> <p>The class with this method compiles, but gives a warning "unchecked":</p> <pre><code>&lt;B extends BitSet&gt; void doStuff(LinkedList&lt;B&gt; list) { B board = list.getFirst(); B cloneBoard; cloneBoard = (B) board.clone(); //unchecked } </code></pre> <p>This compiles without warning:</p> <pre><code>&lt;B extends BitSet&gt; void doStuff(LinkedList&lt;B&gt; list) { B board = list.getFirst(); @SuppressWarnings("unchecked") B cloneBoard = (B) board.clone(); } </code></pre> <p>This doesn't compile but marks cloneBoard with an error:</p> <pre><code>&lt;B extends BitSet&gt; void doStuff(LinkedList&lt;B&gt; list) { B board = list.getFirst(); B cloneBoard; @SuppressWarnings("unchecked") cloneBoard = (B) board.clone(); // cloneBoard cannot be resolved to a type; // VariableDeclaratorID expected } </code></pre> <p>In the Sun tutorial on annotations, I couldn't find an answer as to why this is: <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html</a>.</p> <p>The grammar definition didn't help me either, since I'm not quite sure I understand it correctly: <a href="http://java.sun.com/docs/books/jls/third_edition/html/syntax.html#18.1" rel="nofollow">http://java.sun.com/docs/books/jls/third_edition/html/syntax.html#18.1</a></p> <p>It seems to me that what's the problem here is that annotations can be specifically used for variables, but only when they are declared; any later assignment will not be covered by the annotation. Is this correct? Is there a more elegant solution than suppressing unchecked warnings for the whole method?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload