Java equivalent to C# extension methods

Java does not support extension methods.

Instead, you can make a regular static method, or write your own class.


Extension methods are not just static method and not just convenience syntax sugar, in fact they are quite powerful tool. The main thing there is ability to override different methods based on different generic’s parameters instantiation. This is similar to Haskell’s type classes, and in fact, it looks like they are in C# to support C#’s Monads (i.e. LINQ). Even dropping LINQ syntax, I still don’t know any way to implement similar interfaces in Java.

And I don’t think it is possible to implement them in Java, because of Java’s type erasure semantics of generics parameters.


Project Lombok provides an annotation @ExtensionMethod that can be used to achieve the functionality you are asking for.