Why methods in interface cannot be static




















Or, to put in more completely, If I want to call a method without an instance, but knowing the class, how can I have it resolved based upon the instance that I don't have.

It is right, static methods since Java 8 are allowed in interfaces, but your example still won't work. You cannot just define a static method: you have to implement it or you will obtain a compilation error. Several answers have discussed the problems with the concept of overridable static methods. However sometimes you come across a pattern where it seems like that's just what you want to use.

For example, I work with an object-relational layer that has value objects, but also has commands for manipulating the value objects. For various reasons, each value object class has to define some static methods that let the framework find the command instance. For example, to create a Person you'd do:. This is fairly convenient, however it has its problems; notably the existence of the static methods can not be enforced in the interface. An overridable static method in the interface would be exactly what we'd need, if only it could work somehow.

EJBs solve this problem by having a Home interface; each object knows how to find its Home and the Home contains the "static" methods. This way the "static" methods can be overridden as needed, and you don't clutter up the normal it's called "Remote" interface with methods that don't apply to an instance of your bean. Just make the normal interface specify a "getHome " method. Return an instance of the Home object which could be a singleton, I suppose and the caller can perform operations that affect all Person objects.

All methods in an interface are explicitly abstract and hence you cannot define them as static because static methods cannot be abstract. Well, without generics, static interfaces are useless because all static method calls are resolved at compile time.

So, there's no real use for them. With generics, they have use -- with or without a default implementation. Obviously there would need to be overriding and so on. However, my guess is that such usage wasn't very OO as the other answers point out obtusely and hence wasn't considered worth the effort they'd require to implement usefully. An interface can never be dereferenced statically, e.

An interface is always dereferenced via a variable that refers to an instance of a subclass of the interface. Thus, an interface reference can never know which subclass it refers to without an instance of its subclass. Thus the closest approximation to a static method in an interface would be a non-static method that ignores "this", i. At the low-level abstraction, every non-static method after lookup in any vtable is really just a function with class scope that takes "this" as an implicit formal parameter.

See Scala's singleton object and interoperability with Java as evidence of that concept. And thus every static method is a function with class scope that does not take a "this" parameter. Thus normally a static method can be called statically, but as previously stated, an interface has no implementation is abstract.

Thus to get closest approximation to a static method in an interface, is to use a non-static method, then don't access any of the non-static instance members. There would be no possible performance benefit any other way, because there is no way to statically link at compile-time a ISomething. The only benefit I see of a static method in an interface is that it would not input i.

This would declare implicitly that the function that doesn't access "this", is immutate and not even readonly with respect to its containing class.

But a declaration of "static" in an interface ISomething would also confuse people who tried to access it with ISomething. I had to think about it for a while to get the correct understanding. The way to get a mutable static field in an interface is use non-static getter and setter methods in an interface, to access that static field that in the subclass. Sidenote, apparently immutable statics can be declared in a Java interface with static final. Interfaces just provide a list of things a class will provide, not an actual implementation of those things, which is what your static item is.

You can't define static methods in an interface because static methods belongs to a class not to an instance of class, and interfaces are not Classes. Read more here. Something that could be implemented is static interface instead of static method in an interface. All classes implementing a given static interface should implement the corresponding static methods. You could get static interface SI from any Class clazz using. This would be useful for factory design pattern for example because you can get or check the implementation of SI static methods implementation from a compile time unknown class!

A dynamic dispatch is necessary and you can override the static methods if not final of a class by extending it when called through the static interface. Obviously, these methods can only access static variables of their class. While I realize that Java 8 resolves this issue, I thought I'd chime in with a scenario I am currently working on locked into using Java 7 where being able to specify static methods in an interface would be helpful.

I have several enum definitions where I've defined "id" and "displayName" fields along with helper methods evaluating the values for various reasons. Implementing an interface allows me to ensure that the getter methods are in place but not the static helper methods. Being an enum, there really isn't a clean way to offload the helper methods into an inherited abstract class or something of the like so the methods have to be defined in the enum itself.

Also because it is an enum, you wouldn't ever be able to actually pass it as an instanced object and treat it as the interface type, but being able to require the existence of the static helper methods through an interface is what I like about it being supported in Java 8.

Hence a implementing class' static method would be called underneath, but the invoker class does not know which. How to know it? It has no instantiation to guess that! Interfaces were thought to be used when working with objects.

This way, an object is instantiated from a particular class, so this last matter is solved. The invoking class need not know which particular class is because the instantiation may be done by a third class.

So the invoking class knows only the interface. If we want this to be extended to static methods, we should have the possibility to especify an implementing class before, then pass a reference to the invoking class. This could use the class through the static methods in the interface.

But what is the differente between this reference and an object? We just need an object representing what it was the class. Now, the object represents the old class, and could implement a new interface including the old static methods - those are now non-static. Metaclasses serve for this purpose.

You may try the class Class of Java. But the problem is that Java is not flexible enough for this. You can not declare a method in the class object of an interface. But then you would have to first create an object to call the method. To solve this : error: missing method body, or declare abstract static void main String[] args ;. I think java does not have static interface methods because you do not need them.

You may think you do, but How would you use them? If you want to call them like. If you are actually going to use first way, but just want to enforce each implementation to have such static method, then it is really a coding convention, not a contract between instance that implements an interface and calling code.

Interfaces allow you to define contract between instance of class that implement the interface and calling code. And java helps you to be sure that this contract is not violated, so you can rely on it and don't worry what class implements this contract, just "someone who signed a contract" is enough.

In case of static interfaces your code. What is the need of static method in interface, static methods are used basically when you don't have to create an instance of object whole idea of interface is to bring in OOP concepts with introduction of static method you're diverting from concept.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Why can't I define a static method in a Java interface? Ask Question. Asked 12 years, 9 months ago. Active 29 days ago. Viewed k times. Additionally an interface itself has no code, it's just a type definition. In order to have a static member space would need to be allocated to store the code.

That isn't how interfaces work. Which implementation should it call? If you say it should look at the runtime type of foo and call that version then you are basically saying it should call the method associated with that instance, hence it is an instance method. The compiler cannot determine which version to call without having the instance provided and therefore only instance methods would work here. You could argue that you should be able to call IFoo.

Bar instead, which is how static methods work elsewhere. The problem here is that there can be only 1 implementation of IFoo. Bar, who will provide it? This also means that any implementation of IFoo must properly work with the static method. Note that you can have static methods that work with interfaces, you just cannot define them within the interface because there is no way to correctly reference them. In your specific scenario I would have gone this route if I needed a static method.

This makes it clear which implementation of Bar you want and, under the hood, it can still use the interface-specific methods. But the static variable, before entering the main function, the variable can be used at any where. If your issue has been resolved, please remember to close your thread by marking useful posts as answer that can be helpful for other person with same issue. Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not.

Similarly, we can define static methods inside interfaces to use them as general utility methods. Since we can have static method starting from Java 8, we can define main method inside an interface without violating any rule of java. For example, below code is valid.

For example, below codes are also valid from the previous version of java. For abstract class it is valid from Java 1. Here we are talking about static methods inside Interfaces only. They will be called with the name of Interfaces only. For example, observe the below code. However we can declare methods with the same signature in the implementing classes. They are valid but we will not consider them as overridden methods. Hence in this scenario method overriding concept is not valid.

We will not consider them as an overridden method but general method only. An interface declaration can contain abstract methods, default methods, static methods and constant definitions. The only methods that have implementations are default and static methods. A class that implements an interface must implement all the abstract methods declared in the interface.



0コメント

  • 1000 / 1000