my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interfaces with static and synchronized methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interfaces with static and synchronized methods" Watch "Interfaces with static and synchronized methods" New topic
Author

Interfaces with static and synchronized methods

Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
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
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
I found this on Jguru.com. Here's the link to the same qusetion:
http://www.jguru.com/faq/view.jsp?EID=88225
Do you agree with the answer?
Darren Abbruzzese, Oct 10, 2001

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.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
That's a good explanation indeed
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Interfaces with static and synchronized methods
 
Similar Threads
Implicit properties of interface members...
Interfaces Cannot have static methods??
Why finder methods are not static?
interfaces and locks
Interface Ques