• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Sort of 'extension methods' in Java?

 
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think what I want is exactly extension methods, which is why it is in quotation marks.
But the idea is like this:

As far as I know, this isn't possible in Java, but is there anything that is close to this?
 
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain what you want to do; that is very weird code. Please tell us what you mean by an extension method.
What is the point of an identity method, which is what the first method is?
The second method won't compile because the keyword this is in an inappropriate context. Also, if you look up the behaviour of the + operator in the Java┬« Language Specification (=JLS), you will find out that you don't need to call toString(). 1: Theoperator calls it behind the scenes anyway. 2: If you pass null as the second argument, you won't have an exception thrown.
 
Saloon Keeper
Posts: 9714
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All sorts of reasons that this isn't possible in Java: String is immutable and final and more. The closest I've come to it is a wrapper or util class.
 
Campbell Ritchie
Marshal
Posts: 76811
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't add methods to a String object because the String class is final.

And Merry Christmas
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please explain what you want to do; that is very weird code. Please tell us what you mean by an extension method.
What is the point of an identity method, which is what the first method is?


An extension method as in the line:
That won't compile - 'The method SomeExtraMethod(int) is undefined for the type String'
Or better, similar to the lombok extension method https://projectlombok.org/features/experimental/ExtensionMethod, but in vanilla java.
They do provide a vanilla Java example but I don't like having to do what they did where you need both the class name, and the method name.

Campbell Ritchie wrote:The second method won't compile because the keyword this is in an inappropriate context.


I know, it was more of a pseudocoded idea. It won't compile because of many things.

Campbell Ritchie wrote:Also, if you look up the behaviour of the + operator in the Java┬« Language Specification (=JLS), you will find out that you don't need to call toString(). 1: Theoperator calls it behind the scenes anyway. 2: If you pass null as the second argument, you won't have an exception thrown.


Good to know. Since it won't compile I couldn't really test, but now I know.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can't add methods to a String object because the String class is final.

And Merry Christmas



I should have made it clear I know that - it was just an example.

And Merry Christmas to you as well!
 
Story like this gets better after being told a few times. Or maybe it's just a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic