Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will Java have a way for non-library developers to use extension methods?

C#'s extension methods are great for adding syntactic sugar. Java extension methods are great for allowing library developers to add methods to their interfaces.

I am a non-library Java developer and know I will reap a lot of benefits from getting new functionality from libraries, but I would still like to have the syntactic sugar capabilities of C# extension methods.

Is this, or will this be possible in future versions of Java?

eg: I would like to add methods to the String class...

String data = StringUtils.capitalize("abcd");  // instead of this
String data = "abcd".capitalize()  // I would like to do this

Please don't focus on this particular example, I am only showing the class of functionality I want to be able to achieve.

like image 897
Bob Thule Avatar asked Oct 29 '12 16:10

Bob Thule


1 Answers

Java does not have this feature, nor is it likely to have it anytime soon.

Groovy however does have a very similar feature, and it also runs on the JVM. Perhaps that's an option.

like image 83
Sean Patrick Floyd Avatar answered Oct 07 '22 08:10

Sean Patrick Floyd