This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I've seen some different explanations on why interfaces cannot have static methods nor synchronized methods but none of them have sounded convincing. Does anyone know the definitive reasons why an interface cannot have static methods nor synchronized methods? Thanks.
Sun Certified J2EE Architect for the J2EE Platform (Part 1)<br />Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform
As far as I know, I do not have the definitive answer, but I think that I have a good one. As you know, the JLS simply states that static methods can not be abstract. If I had to speculate on Sun's reasoning for that design choice, I would say that it is because a subclass does not override a static method declared in a super class. Instead, a subclass only hides a super class method. The differences between overriding and hiding are probably the motivation for the restriction. The reference to a static method is resolved at compile time based on the type of the reference. If the reference has the type of the base class, then the base class method will be invoked at run time even if the reference object has a sub class type that hides the base class method. If the base class method is abstract, then the method invoked at runtime would have no implementation.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Joined: Jul 02, 2002
I think that the following question from my mock exam does a good job of illustrating the differences between method overriding and method hiding.
The correct answer is b. Class Q hides printS1 and overrides printS2. Therefore, the superclass implementation of printS1 is invoked and the sub class implementation of printS2 is invoked. If P.printS2 had been abstract, then the program would still work. However, if the static method P.printS1 had been abstract, then the invoked method would have no implementation.
Synchronized methods are not allowed to be declared within an interface just because synchronization is an implementation issue. Since a method is not implemented in an interface there is no reason to put that keyword there. From JLS 9.4 Abstract Method Declarations
Note that a method declared in an interface must not be declared strictfp or native or synchronized, or a compile-time error occurs, because those keywords describe implementation properties rather than interface properties. However, a method declared in an interface may be implemented by a method that is declared strictfp or native or synchronized in a class that implements the interface.
The reason why you can't have a static method in an interface lies in the way Java resolves static references. Java will not bother looking for an instance of a class when attempting to execute a static method. This is because static methods are not instance dependent and hence can be executed straight from the class file. Given that all methods in an interface are abstract, the VM would have to look for a particular implementation of the interface in order to find the code behind the static method so that it could be executed. This then contradicts how static method resolution works and would introduce an inconsistency into the language.