File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why... interface methods can't be static? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why... interface methods can Watch "Why... interface methods can New topic
Author

Why... interface methods can't be static?

Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61
Abstract class can have static methods...
while it is illegal for interfaces...Why?

What rules are opposing to having static methods in interfaces?

Please clarify me..with your replies..

Thanks
hatim osman
Ranch Hand

Joined: Jun 10, 2005
Posts: 105
hi..
Because more the one class can implement the interface, and each of them can have it's own implementation which is not necessarily match those of others.Ex. if the MouseListener methodes were static, then you can have more than one listner throughout the application life time.
Hope this help
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
An interface contains no implementations for its method declarations, the methods have to be provided by classes that implement the interface. The classes provide the implementation of the methods by overriding the methods specified in the interface. static methods cannot be overridden. So it is useless declaring a static method in an interface, because nothing can provide its implementation.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61
Thank you hatim and Barry...

You may misunderstood my question...

By Barry:
static methods cannot be overridden. So it is useless declaring a static method in an interface, because nothing can provide its implementation.


Is there any rule static Vs interface...just like rule abstract Vs final
OR
Java Developers just thinks that..there is no need of static methods in interface...and put that reststriction...interface methods can't have body..
and given a STRONG Defination for interface...


Is there any problem... if we have static methods too along with method declaration in interface... (of course if Java Developers allowed)..

Thanks in Advacne....Waiting....
Barend Garvelink
Ranch Hand

Joined: Dec 07, 2004
Posts: 64
Originally posted by Ramakrishna Nalla:
Thank you hatim and Barry...

You may misunderstood my question...

Actually, Ramakrishna, you misunderstood the answer.

Static methods are bound to the class itself, not to the objects made of that class. In other words, If a static method is defined in another class, it's a different method, not an override. Which brings us to what Barry said:

Static methods cannot be overridden. So it is useless declaring a static method in an interface, because nothing can provide its implementation.


If you'd put a static method in an interface, you'd define a method that can not have a method body. As Barry so eloquently put it: that's a useless method.


SCJP 1.4, SCJD 1.4, SCWCD 1.3, ICSD:Websphere 5.1
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 526

Hi Ramakrishna,

think about it for a second there...
Interface methods are meant to be implemented.

An interface defines a contract between itself and implementing classes, saying what they must implement and not how. But if you where allowed to have a static abstract method, what good would it be if its going to be part of the class and not any particular instance.

This would completly put polymorphism back in its box (and where would OO programing be without polymorphism?)

hth.
Marzo.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Ramakrishna Nalla
Ranch Hand

Joined: Apr 21, 2005
Posts: 61
So much Thanks Barend..for correcting me...and also Marzo Kaahn...
Once again thanks to Barry...

I just think like below code hence that question arises for me..


Thanks to ALL..for your valuable replies..
Wensheng Li
Greenhorn

Joined: Jul 08, 2005
Posts: 16
By the way, abstract class can have static methods only for those methods which are NOT abstract themselves.For example, it won't compile if you write:

abstract class A{
static abstract void method();
}


SCWCD 1.4(In progress)<br />SCJP 5.0<br />IBM Certified Solutions Expert for Webshpere Studio, V3.5 (retired)
Pui Shing Lee
Greenhorn

Joined: Jul 23, 2005
Posts: 10
hi, just read the posts of this thread, and I have one question
hope anyone can explain to me:
Is there any restrictions on method defition within interface?
Kanchan K Bulbule
Greenhorn

Joined: Jul 19, 2005
Posts: 20
Hey ,

Interface methods can have either default or public access.
As they will be implemented by other classes complier complains when
modifiers like static , final and protected are specified for them.

Interface can also contain field but they will be final and static implicitly.


Cheers
kanchan

Preparing for SCJP1.4
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392
Hi Kanchan

Interface methods can have either default or public access.

Every method in an interface is implicitly public and abstract. See JLS3 9.4 Abstract Method Declarations.

Interface can also contain field but they will be final and static implicitly.

As for the fields, they're implicitly public, static and final. See JLS3 9.3 Field (Constant) Declarations.

Joyce
[ July 26, 2005: Message edited by: Joyce Lee ]
Kanchan K Bulbule
Greenhorn

Joined: Jul 19, 2005
Posts: 20
Thnks Joyce.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why... interface methods can't be static?