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 76k
  • Answers 76k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You don't need a ControlTemplate for this. You can accomplish… May 11, 2026 at 3:07 pm
  • added an answer Check out Big-O for Eight Year Olds? May 11, 2026 at 3:07 pm
  • added an answer Assuming that your model relationships are set up correctly*, it's… May 11, 2026 at 3:07 pm

Related Questions

I want to make a local instance of a Java Scanner class in a
I've read the manual many times, I've scoured the posts offered by Google on
The "goto" statement comes straight out of ASM or any other assembler language. Here's
Okay this is a real headscratcher. I have an application which calls a web

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.