Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 90375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:51:48+00:00 2026-05-10T22:51:48+00:00

Why does it (apparently) make a difference whether I pass null as an argument

  • 0

Why does it (apparently) make a difference whether I pass null as an argument directly, or pass an Object that I assigned the value null?

Object testVal = null; test.foo(testVal);    // dispatched to foo(Object) // test.foo(null);    // compilation problem -> 'The method foo(String) is ambiguous'     public void foo(String arg) { // More-specific     System.out.println('foo(String)'); }  public void foo(Object arg) { // Generic     System.out.println('foo(Object)'); } 

In other words, why is the (commented-out) second call to foo(...) not dispatched to foo(Object)?

Update: I use Java 1.6. I could compile Hemal’s code without problems, but mine still doesn’t compile. The only difference I see is that Hemal’s methods are static while mine are not. But I really don’t see why this should make a difference…?

Update 2: Solved. I had another method foo(Runnable) in my class, so the dispatcher couldn’t unambiguously select the single most specific method. (See my comment in Hemal’s second answer.) Thanks everyone for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:51:49+00:00Added an answer on May 10, 2026 at 10:51 pm

    Which version of Java are you using? With 1.6.0_11 the code (pasted below) compiles and runs.

    I am sure its obvious why foo(testVal) goes to foo(Object).

    The reason why foo(null) goes to foo(String) is a little complex. The constant null is of type nulltype, which is a subtype of all types. So, this nulltype extends String, which extends Object.

    When you call foo(null) compiler looks for the overloaded method with most specific type. Since String is more specific then Object that is the method that gets called.

    If you had another overload that was as specific as String, say foo(Integer) then you would get a ambiguous overload error.

    class NullType {    public static final void main(final String[] args) {     foo();   }    static void foo()   {     Object testVal = null;     foo(testVal);    // dispatched to foo(Object)     foo(null);    // compilation problem -> 'The method foo(String) is ambiguous'      }    public static void foo(String arg) { // More-specific     System.out.println('foo(String)');   }    public static void foo(Object arg) { // Generic     System.out.println('foo(Object)');   }  } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 179k
  • Answers 179k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This looks a little bizarre: Chunk:: Chunk* GetNextFilledChunk() { Is… May 12, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer Dennis, In case you haven't already found an answer to… May 12, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer Or you can make it not do that. See the… May 12, 2026 at 3:51 pm

Related Questions

Apparently this function in SDL_Mixer keeps dying, and I'm not sure why. Does anyone
I want to make a local instance of a Java Scanner class in a
I'm experimenting with writing ActiveX controls and noticed that I can't seem to create
I've read the manual many times, I've scoured the posts offered by Google on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.