This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Pointer to a Method  RSS feed

 
Greenhorn
Posts: 24
Linux Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • 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
Linux Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I will look into Pattern as you suggest.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!