@override annotation in JDK 1.6
Solution 1
You probably need to set the compiler compliance level in eclipse. This can be found in Window->Preferences->Java->Compiler
If the compiler preferences are still set to 1.5 the compiler will barf on the override annotation.
Edit: Also check compiler compliance level on a per project basis if you've set those to anything else than default.
Solution 2
@Override works on method implementation since java 1.6.
Resources :
- Sun's forums - Java Programming - Should @Override apply to implementation of interface/abstract methods?
- dertompson.com - @Override specification changes in Java 6
- The Former Weblog of Peter Ahé - @Override snafu
On the same topic :
Solution 3
The Java Compiler settings can be at multiple places based on the configuration You choose, One way is to Window->Preferences->Java->Compiler, change that to 1.6 minimum, if it was set to some earlier version. Another way is Right Click on Project-> Properties ->Java Compiler ->JDK Compliance ->Select JDK1.6 minimum, click apply.
After you make the changes, let the project build, it builds and take the changes into affect.
If none of the above options work - Try adding the rt.jar to classpath, it will fix the problem.
Solution 4
The @Override annotation changed in Java 1.6 version. In Java 1.5, the compiler didn't allow @Override annotation on implemented interface methods, from 1.6 it does.
You must change java compiler version in properties project -> Java Compiler
Comments
-
Veera almost 2 years
I'm using JDK1.6. When I implement an interface and in the implementing class, if I give
@override
before my function names, Eclipse throws an compilation error. i.e. below code is wrong according to Eclipse.public class SomeListener implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent arg0) { // code } /* other overridden methods here */ }
If I remove
@Override
annotation, then the code compiles fine. Does it mean that JDK1.6 does not require us to prefix the@override
annotation anymore? -
Jacob Tomaw over 13 yearsThis should be a comment to the question.
-
Powerlord about 12 years+1: I just got burned by this. Apparently one of my coworkers who uses Eclipse has his project set to 1.6, despite Maven setting the compiler version to 1.5 (and me using m2e to import it as a Maven project...).
-
T A almost 11 yearsIn my case it was because in the pom.xml, for maven build, the plugin maven-compiler-plugin was configured to work for 1.5 version java source and target. Changing the values in it to the following fixed it: <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin>