This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why an Interface cannot have static method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why an Interface cannot have static method?" Watch "why an Interface cannot have static method?" New topic
Author

why an Interface cannot have static method?

Rama Lakshmi
Greenhorn

Joined: Mar 31, 2006
Posts: 27
Hi

Anyone can tell the reasons for not to have static methods in
a interface.

Thanks and Regards
Rama
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

static mathods are class level method, meaning it belongs to the class it defined and implemented. But if we are allowed to have static methods in interfaces which class would the actual implementation(where we actually call the method) belongs to?


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
dee anderson
Greenhorn

Joined: Oct 10, 2008
Posts: 16
All methods in an interface are purely abstract, they do not contain any body code. A static method can be called without making an instance of the class that contains it (class.staticmethod()).
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
This has been discussed at length before. Please read this thread.


Preparing for SCJP 6.0
Rama Lakshmi
Greenhorn

Joined: Mar 31, 2006
Posts: 27
ThanQ Guys for a quick responce

Ragards
Rama
Tuna Töre
Ranch Hand

Joined: Aug 17, 2008
Posts: 219

The other reason is

as a Rule

interface methods must be overridden, but on the other hand if you make your
method static in a interface that means you cannot override a method because
static methods cannot be overriden.

so you cannot add a static method in an interface
but you can put an object in an interface and access it with interface reference(like static way) like ;



public interface Bird
{
Wings one = new Wings();
}

public class Wings {

}

public class Test {

public static void main(String[] args) {

Wings w = Bird.s; // interface access

}

}

[ October 15, 2008: Message edited by: Anut Walidera ]
[ October 15, 2008: Message edited by: Anut Walidera ]

blog: http://tunatore.wordpress.com
SCJP 6.0 + SCWCD 1.5
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why an Interface cannot have static method?
 
Similar Threads
Static methods in Java Interfaces?
static interface??
abstract class(all abstract methods) vs interface
can i have a static interface
Abstract Class and Initialisation block