Skip to main content


Showing posts from December, 2010

Order matters

Every so often I come across these simple things that after thinking about them I kick myself for not thinking about and knowing sooner. Earlier today was one of those moments!

It might seem natural, assumed, even obvious that for many method calls it doesn't actually matter what way around things go. So take "asd".equals("dsa"); - does it ever make a difference?

In this case, no - but what about if we introduce variables?

String str = "asd";

Again, no difference. Now let's be a bit sneaky however and swap str to null (you may see where this is going!)

String str = null;

Ahah! This time we (somewhat obviously) get a NPE on the third line. But the second line executes without any problems at all (also rather obviously - we're not dereferencing null here!)


The solution

You might expect it to print out 104, "10" being the sum of the fields and then "4" being the number of fields. You would of course be wrong :-)

If you run the following code you won't get a number out at all, you'll get an exception:

Exception in thread "main" java.lang.IllegalArgumentException: Attempt to get test.Outer field "test.Outer$Inner.this$0" with illegal data type conversion to int
at sun.reflect.UnsafeFieldAccessorImpl.newGetIllegalArgumentException(
at sun.reflect.UnsafeFieldAccessorImpl.newGetIntIllegalArgumentException(
at sun.reflect.UnsafeQualifiedObjectFieldAccessorImpl.getInt(
at java.lang.reflect.Field.getInt(
at test.Outer.(
at test.Outer.main(

The key here is actually down to an implementation detail of Java - specifically h…