aspose file tools*
The moose likes Beginning Java and the fly likes Static methods in Java Interfaces? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Static methods in Java Interfaces?" Watch "Static methods in Java Interfaces?" New topic
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
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3225
    
  49

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 ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- 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
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3225
    
  49

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
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3225
    
  49

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
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3225
    
  49

Rajiv Chelsea wrote:Is implementing an interface method in a class, same as trying to override it?

Of course
pardeepkumar devgon
Greenhorn

Joined: Aug 23, 2013
Posts: 8

With the release of Java 8 you can now have static and default method in an interface


Pardeep.Kumar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methods in Java Interfaces?