This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Hi Rajaraman, The following statement in your main method creates ambiguity new Sample().meth(null);
This is because null is not an instance of any class, the compiler cannot resolve which overloaded method to call to at compile time.
You can check this with the instanceof operator null instanceof Sample // false null instaneof Object //false null instaceof String //false In other words, null is not an instanceof any class. But null can be casted to any class you like. That is the reason you get a NullPointerException with any object and can choose to return null from any method returning an object.
If you modify the above statement as any of the following: new Sample().meth((Sample)null); //In Sample new Sample().meth((Object)null); //In object new Sample().meth((String)null); //In String it will work.