Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 reference to a static method vs. instance method

say I have the following code

public class A {
    int x;
    public boolean is() {return x%2==0;}
    public static boolean is (A a) {return !a.is();}
}

and in another class...

List<A> a = ...
a.stream().filter(b->b.isCool());
a.stream().filter(A::is); 
//would be equivalent if the static method is(A a) did not exist

the question is how do I refer to the instance method version using the A::is type notation? Thanks a lot

like image 824
Will Sherwood Avatar asked Apr 14 '14 04:04

Will Sherwood


People also ask

When would you use a static method vs an instance method in a class?

Instance method are methods which require an object of its class to be created before it can be called. Static methods are the methods in Java that can be called without creating an object of class.

Is an instance method able to reference access static data Why or why not?

A static method cannot access a class's instance variables and instance methods, because a static method can be called even when no objects of the class have been instantiated.

What is the difference between static class method and instance method in Java?

Also static methods exist as a single copy for a class while instance methods exist as multiple copies depending on the number of instances created for that particular class. Static methods can't access instance methods/variables directly while instance methods can access static variables and static methods directly.

What is the difference between instance methods class methods and static methods?

Instance methods need a class instance and can access the instance through self . Class methods don't need a class instance. They can't access the instance ( self ) but they have access to the class itself via cls . Static methods don't have access to cls or self .


1 Answers

In your example, both the static and the non-static method are applicable for the target type of the filter method. In this case, you can't use a method reference, because the ambiguity can not be resolved. See §15.13.1 Compile-Time Declaration of a Method Reference for details, in particular the following quote and the examples below:

If the first search produces a static method, and no non-static method is applicable [..], then the compile-time declaration is the result of the first search. Otherwise, if no static method is applicable [..], and the second search produces a non-static method, then the compile-time declaration is the result of the second search. Otherwise, there is no compile-time declaration.

In this case, you can use a lambda expression instead of a method reference:

a.stream().filter(item -> A.is(item));

The above rule regarding the search for static and non-static methods is somewhat special, because it doesn't matter, which method is the better fit. Even if the static method would take an Object instead of A, it's still ambiguous. For that reason, I recommend as a general guideline: If there are several methods with the same name in a class (including methods inherited from base classes):

  • All methods should have the same access modifiers,
  • All methods should have the same final and abstract modifiers,
  • And all methods should have the same static modifier
like image 65
nosid Avatar answered Oct 10 '22 11:10

nosid