• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods in Java Interfaces?

 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why can't we have static methods in an interface?
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4498
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhijeet Ravankar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4498
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4498
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods cannot be *overridden* (you can hide them, though). Have a look at this this FAQ.
 
Abhijeet Ravankar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi devaka

As your reply suggests...

Is implementing an interface method in a class, same as trying to override it?
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4498
267
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Chelsea wrote:Is implementing an interface method in a class, same as trying to override it?

Of course
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface is a reference type like class that can contain only constants,method signature ,default methods,static method and nested types.Method body exist only for default and static methods
Reason for introducing this change is whenever a method is added in interface all class implementing it must be modified so now by adding two more type method .problem is solved
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic