Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Static import and method overloading

843793Nov 16 2006 — edited Nov 17 2006
When I use a static import to import a static method of signature s1
in a class that has a method of the same name, but a different signature s2, I get a compile error when I reference the imported method.

Example:
// Class Provider provides some static method
package staticimport;

public class Provider {

  public static void staticMethod(String text) {
    System.out.println(text);
  }
}

// Class Consumer references the method provided by the Provider class
package staticimport;

import static staticimport.Provider.staticMethod;
public class Consumer {

  public static void staticMethod() {
    staticMethod("cannotcompile"); // Gives compile error
  }
}
The compile error in this example is:
The method staticMethod() in the type Consumer is not applicable for the
arguments (String)

So is it true that
import static <method_name> only "considers" the method's simple name rather than the full signature and shadows all methods (both static and non-static) of the same simple name within the compilation unit, irrespective of the signature, leading to the above error?

Related topics are
http://forum.java.sun.com/thread.jspa?forumID=316&threadID=533658
http://forum.java.sun.com/thread.jspa?forumID=316&threadID=458109
http://forum.java.sun.com/thread.jspa?forumID=316&threadID=458109
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 15 2006
Added on Nov 16 2006
5 comments
561 views