Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Not with that syntax. That syntax declares 'bar' as an instance variable. and 'foo' there is a null pointer, so trying to dereference it like that should produce a NullPointerException.

Sure, you could change the declaration of 'bar' to include 'static', and change the access of 'bar' from 'foo.bar' to 'Foo.bar', but... that's not what the code says.



I think you can do the same in C++, or at least something very similar. If a method isn't virtual and doesn't touch the object's fields, you can invoke it on null quite happily.

Doesn't Go do this too?


I'm pretty sure that would be an "Undefined Behaviour" in C++. And you can make lots of "Wat" moments by invoking UB.


Yeah, but it is a field, not a method.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: