Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I "static import" an "equals" method in Java?

I like using this method here:

org.apache.commons.lang.ObjectUtils.equals(Object object1, Object object2) 

The only drawback (compared to Google Guava, for instance), is that I cannot static import the method. I.e. this is useless:

import static org.apache.commons.lang.ObjectUtils.equals; 

... as my Eclipse compiler will not correctly link that method when writing

equals(obj1, obj2); 

The error is:

The method equals(Object) in the type Object is not applicable for the arguments (..., ...)

Why is that? Is my statically imported method not applicable if there is a method with the same name (but not the same signature) in any of the super types? Is this formally specified in the JLS? Or some Eclipse compiler issue?

UPDATE

This doesn't work either:

import static org.apache.commons.lang.ObjectUtils.defaultIfNull;  public class Test {   void test() {     defaultIfNull(null, null);     // ^^ compilation error here   }    void defaultIfNull() {   } } 

javac error message:

Test.java:5: defaultIfNull() in Test cannot be applied to (<nulltype>,<nulltype>) defaultIfNull(null, null);     ^ 1 error 
like image 513
Lukas Eder Avatar asked Oct 25 '11 14:10

Lukas Eder


People also ask

Can you import a static method in Java?

Static import is a feature introduced in the Java programming language that allows members (fields and methods) which have been scoped within their container class as public static , to be used in Java code without specifying the class in which the field has been defined.

Can you import a static method?

With the help of import, we are able to access classes and interfaces which are present in any package. But using static import, we can access all the static members (variables and methods) of a class directly without explicitly calling class name.

Why are static imports bad Java?

by Joshua Bloch.) This is considered bad Java programming practice because when a class implements an interface, it becomes part of the class's public API. Implementation details, such as using the static members of another class, should not leak into public APIs.


1 Answers

As per Java Language Specification

  1. If a single-static-import declaration imports a member whose simple name is n, and the compilation unit also contains a single-type-import declaration that imports a type whose simple name is n, a compile-time error occurs. (This error occurs even if both declarations refer to the same type, on the grounds that it is confusing to use two different mechanisms to redundantly import the same type.)
  2. If a single-static-import declaration imports a member whose simple name is n, and the compilation unit also declares a top level type whose simple name is n, a compile-time error occurs.

So in your case its point 2 mentioned above is the reason you are getting compile time error. so even if method signatures are different if there names are same its a compile time error.

static import JSR and JLS

like image 117
dpsdce Avatar answered Sep 21 '22 12:09

dpsdce