Veera
Veera

Reputation: 33172

@override annotation in JDK 1.6

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?

Upvotes: 10

Views: 15141

Answers (7)

Borja
Borja

Reputation: 3610

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.

Java Compiler

You must change java compiler version in properties project -> Java Compiler

Upvotes: 1

kanaparthikiran
kanaparthikiran

Reputation: 523

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.

Upvotes: 2

Noel M
Noel M

Reputation: 16116

It sounds like your compiler is set for Java 5, when @Override on interfaces wasn't allowed.

Upvotes: 0

Manuel Selva
Manuel Selva

Reputation: 19050

No the @Override annotation is still used. You should check that the contextDestroyed method is really present in the ServletContextListener interface, and check the imported package for this interface.

Upvotes: 0

Riaan Cornelius
Riaan Cornelius

Reputation: 1953

JDK1.6 definitely supports it. I'm not sure why you would have issues.

What error are you seeing? The only thing I can think of is to make sure that you are using the correct JDK in your project settings. Maybe you are compiling against an older JDK?

Upvotes: 0

Gennadiy
Gennadiy

Reputation: 18207

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.

Upvotes: 27

Related Questions