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

Thankfully Java 5 added 'import static' which lets you pretend classes are namespaces.

http://docs.oracle.com/javase/1.5.0/docs/guide/language/stat...




Thanks for that info. Anyone know if C# has something similar?

From the linked docs: So when should you use static import? Very sparingly!

To me, it sort of confirms the point that these inelegant features are necessary because the language doesn't support fundamental primitives like plain functions and named constants without a superfluous object definition.


The CLI itself actually supports unadorned functions, but C# doesn't provide a way of writing them, or using directly them without resorting to reflection. I'm pretty sure C++/CLI lets you use and write standalone functions in managed code, and come to think of it I imagine F# would too. Also it's not an exact equivalent, but C# extension methods can often be used instead of static methods.


You can mark a class as static, then it will only allow static functions or members:

static class Math { static int Pow(int a,int b) {...} }




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

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

Search: