• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pointer to a Method

 
Greenhorn
Posts: 24
Postgres Database VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings:

One of the neat time saving code tricks I used to do in C was to store the address of a function in a pointer for later use. It was slick to be able to decide which function needed to be used somewhere else and then have a block that simply dereferenced the pointer and called the correct function.

During my short stint in the world I Java I have already seen a couple occasions where that functionality would be nice to have. Can one do this kind of thing in Java? I have experimented with this but to no avail. Just wondering if any of the experts here know of a way.

Thanks...
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That feature is not available as such. However, there's the java.lang.reflect.Method object, but you shouldn't use reflection unless you have no choice. A better way is to use an interface. The Command Pattern is a reasonable approximation to what you're talking about, at least for some use cases.
 
Terry Tucker
Greenhorn
Posts: 24
Postgres Database VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. I will look into Pattern as you suggest.
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic