| Author |
Static methods in Java Interfaces?
|
Rajiv Chelsea
Ranch Hand
Joined: Jun 15, 2010
Posts: 88
|
|
|
why can't we have static methods in an interface?
|
 |
Nicola Garofalo
Ranch Hand
Joined: Apr 10, 2010
Posts: 308
|
|
A static method is a method related to a class, not to an interface.
In interfaces you define a behaviour that has to be implemented by a class. How do you implement an interface? Overriding all the methods declared by that interface.
But static methods cannot even be overridden...
|
Bye,
Nicola
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
Let's suppose that it was allowed.
Now since printIt() is static, you may call to that method directly as:
TestInterface.printIt();
Now, what could be happen? printIt() has no body!
Fortunately, static methods are not allowed for interfaces.
|
Author of ExamLab (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
|
 |
Abhijeet Ravankar
Ranch Hand
Joined: Mar 15, 2009
Posts: 62
|
|
Simplest explanation is that methods in interfaces are by default "abstract". And we cannot have abstract static methods.
But, I tried this code and it worked!!! (Can we really have static methods in interface!)
and you get output ... program runs! (umm... somebody please explain me too. I am also a bit confused.)
|
 |
Nicola Garofalo
Ranch Hand
Joined: Apr 10, 2010
Posts: 308
|
|
coolName is not a static method, is a static variable. All variables in interfaces are static public and final, i.e. they are constants.
You defined a constant, not a static method.
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
Can we really have static methods in interface!
As Nicola said, what that interface has is a constant attribute named 'coolName' which refers to an object of an anonymous inner class that implements a Runnable interface.
|
 |
Rajiv Chelsea
Ranch Hand
Joined: Jun 15, 2010
Posts: 88
|
|
Hi Devaka
Suppose static methods were allowed in an interface. A class implements that interface , and in that class we would give d definition(body) for
the static method , as for normal interface methods. and then we could call the implemented method..
Why is the above not possible?
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
|
Static methods cannot be *overridden* (you can hide them, though). Have a look at this this FAQ.
|
 |
Abhijeet Ravankar
Ranch Hand
Joined: Mar 15, 2009
Posts: 62
|
|
Yes, its just a constant attribute named 'coolName' which refers to an object of an anonymous inner class that implements a Runnable interface.
Thanks Devaka and Nicola for clearing.
Thanks Rajiv for asking interesting question.
|
 |
Rajiv Chelsea
Ranch Hand
Joined: Jun 15, 2010
Posts: 88
|
|
hi devaka
As your reply suggests...
Is implementing an interface method in a class, same as trying to override it?
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
Rajiv Chelsea wrote:Is implementing an interface method in a class, same as trying to override it?
Of course
|
 |
 |
|
|
subject: Static methods in Java Interfaces?
|
|
|