Results 1 to 7 of 7
  1. #1
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Store methods (or pointers) in variables?

    Hi!
    As the title suggests i want to store methods in variables.

    I remember doing something like it in lua:


    function aFunc()
    --Do something
    end

    variableName = aFunc

    variableName()
    That would call aFunc.

    And c++ has pointer which could act like this?

    Any help appreciated :D

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Store methods (or pointers) in variables?

    You can't do that in Java, but Java can store Interfaces; e.g. you can pass a Runnable around and call its run() method; the functionalities between pointers to methods/functions is similar; Java 8 has lambas (anonymous methods) with everything that comes with them (and most people prefer to ignore).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Xyexs is offline Member
    Join Date
    Apr 2013
    Posts
    36
    Rep Power
    0

    Default Re: Store methods (or pointers) in variables?

    That's a shame ... wellwell :D There is always another way to solver things :D

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Store methods (or pointers) in variables?

    Quote Originally Posted by Xyexs View Post
    That's a shame ... wellwell :D There is always another way to solver things :D
    What is a shame? The lack of method/function pointers? You don't need them when you can pass entire classes (or interace implementations) around. Being able to pass naked methods around is a hack i.m.h.o. A method belongs to an object, you shouldn't be able to rip it out ...

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,801
    Rep Power
    8

    Default Re: Store methods (or pointers) in variables?

    Quote Originally Posted by JosAH View Post
    you shouldn't be able to rip it out ...
    <ebil>
    Unless it's a heart!
    </ebil>

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    Default Re: Store methods (or pointers) in variables?

    Quote Originally Posted by Xyexs View Post
    That's a shame ... wellwell :D There is always another way to solver things :D
    Yes, a cleaner, more readable and far less fragile way.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    ArnavKumar is offline Member
    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Default Re: Store methods (or pointers) in variables?

    I do not think that it is possible but we can store objects to share the methods or variables.
    Object obj1=new Object();
    Object obj2=null;
    obj2=obj1;

Similar Threads

  1. Replies: 7
    Last Post: 11-05-2012, 12:04 AM
  2. How To Store Variables In A Text File That Aren't Constant
    By anthropamorphic in forum New To Java
    Replies: 6
    Last Post: 08-10-2011, 11:56 PM
  3. Store links in text file and read into variables
    By africanhacker in forum New To Java
    Replies: 1
    Last Post: 03-31-2011, 05:18 PM
  4. Replies: 4
    Last Post: 06-09-2008, 04:44 PM

Posting Permissions

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