Results 1 to 9 of 9
  1. #1
    AedonetLIRA is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default Function pointers?

    Hiya, I'm working on something where be able to pass functions around would be extremely useful. But I don't see anywhere if Java can do it? Can it?

    Thanks for any help.
    ~Aedon

    Addendum: I am aware of using interfaces, but I was curious if there was a more direct way to accomplish this.
    Last edited by AedonetLIRA; 01-17-2011 at 04:21 AM. Reason: Addendum 1

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Nope! Java doesn't have closures like that - Ruby does though! :D

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that Java 7 was supposed to have them (Lambda expressions), but I've heard that it's now been delayed until Java 8.

  4. #4
    AedonetLIRA is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by quad64bit View Post
    Nope! Java doesn't have closures like that - Ruby does though! :D
    So does c, but I was hoping to use java.... )-; Thanks

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I'm not sure I like the idea of including it - passing code blocks would be handy in some cases, but its dangerous and encourages lazy design IMHO. Java is one of those languages who's beauty has often been the product of its limitations - making it easy to pick up, and forceful in making you really design your system OOP style. Maybe not as much as ObjC, but its much simpler than ObjC too :D

  6. #6
    AedonetLIRA is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default

    True, but there are times where the usefulness. For instance cycling through a list of actions that an object has and passing it to someone.

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Maybe you can think of a specific example where this wouldn't work, but I think this is usually accomplished using interfaces and passing objects to each other - allowing each object to implement its own actions, but all functioning under a global method call.

  8. #8
    AedonetLIRA is offline Member
    Join Date
    Oct 2010
    Posts
    22
    Rep Power
    0

    Default

    And they do, I'm more thinking of something like uh:

    class obj extends BigObj// method list
    doSomething1()
    doSomething2()
    ...
    doSomethingN()

    And being able to just have an object pass an array of all the doSomethings to whoever need them. I could hard code it, but if an iteration of BigObj has a differing amount of doSomething()'s that need to be used, It would end up being a massive pain.

  9. #9
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Yup, an interface could do it. You could pass an array of the interface to the object, and they could call the method on the interface objects.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. pointers needed please (re: MouseListener)
    By SM2010 in forum New To Java
    Replies: 2
    Last Post: 05-26-2010, 12:17 PM
  2. Does Java support pointers?
    By kthaker in forum New To Java
    Replies: 2
    Last Post: 10-07-2009, 08:16 AM
  3. pointers and wrapper classes
    By becky in forum New To Java
    Replies: 11
    Last Post: 02-07-2009, 03:59 AM
  4. Java pointers? How to...
    By Krooger in forum New To Java
    Replies: 4
    Last Post: 11-04-2008, 08:30 PM
  5. Pointers
    By ravian in forum New To Java
    Replies: 5
    Last Post: 11-28-2007, 01:49 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •