Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: call a function dynamically

  1. #1
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default call a function dynamically

    Hi,

    I want to be able to call dynamically a method, but I don't know how to do it.

    I explain my problem:
    I have a class Element with just some String attributes and the getters and setters.

    In another class Group I have a method
    Java Code:
    getField(String name)
    that returns a String. I want to call the getter of the Element class with the name-parameter of the method getFields. For example if I call
    Java Code:
    this.getFields(header)
    I want to return the result of the method
    Java Code:
    getHeader()
    in the Element class.

    I hope you understand what I mean and of course that you can help me.

    thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: call a function dynamically

    Have a look at the Reflection mechanism; start reading the API for the Class<T> class ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    bigjo is offline Member
    Join Date
    Jul 2011
    Posts
    27
    Rep Power
    0

    Default Re: call a function dynamically

    I find a solution myself with this
    Java Code:
    public String getValue(String parameter){
            return String.valueOf(parameter);
        }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: call a function dynamically

    Quote Originally Posted by bigjo View Post
    I find a solution myself with this
    Java Code:
    public String getValue(String parameter){
            return String.valueOf(parameter);
        }
    Really?

    db
    PhHein likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Dynamically call inherited method
    By joeyvitoro in forum Advanced Java
    Replies: 3
    Last Post: 12-02-2011, 07:37 PM
  2. Dynamically create objects, set value and call
    By buntyindia in forum New To Java
    Replies: 5
    Last Post: 05-25-2011, 05:59 PM
  3. Call a function via URL
    By cali_guy_0024 in forum New To Java
    Replies: 7
    Last Post: 04-10-2011, 05:04 AM
  4. function call error
    By peter_thm in forum New To Java
    Replies: 2
    Last Post: 01-13-2010, 12:57 PM
  5. Replies: 0
    Last Post: 02-17-2008, 08:57 AM

Posting Permissions

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