Results 1 to 9 of 9
  1. #1
    Gelembjuk is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default How to create object dinamically and call methods.

    Hello.

    I want to create object of the class dynamically.
    I have 2 variables.
    Java Code:
    String classname="MyClass";
    String methodname="MyMethod";
    How can i create the object of class that name is in variable classname and call method that name is in variable methodname?

    In php i can just write like this:
    PHP Code:
    $classname="MyClass";
    $methodname="MyMethod";
    $object=new $classname();
    $object->$methodname();
    How to do this in java?

    And if this is possible how can i pass arguments if i have variable like this
    Java Code:
    String args="string:sometext;int:14;";
    or similar. I don't know on coding stage how many arguments are here and types. It must be parsed when running and passed to method.

    Thanks.

  2. #2
    andy_man's Avatar
    andy_man is offline Member
    Join Date
    Oct 2008
    Location
    Yerevan
    Posts
    14
    Rep Power
    0

    Default

    Java Code:
    String classname="MyClass";
    String methodname="MyMethod";
    
    Class c = Class.forName(classname);
    Object o = c.newInstance();
    TEAM = Together Everyone Achieves More :)

  3. #3
    andy_man's Avatar
    andy_man is offline Member
    Join Date
    Oct 2008
    Location
    Yerevan
    Posts
    14
    Rep Power
    0

    Default

    And for invoking the method with the specified name, please look through the java.lang.Class Java API specification, you'll find what you need :)
    TEAM = Together Everyone Achieves More :)

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

    Default

    What you are talking about is called Reflection, and from the little I know about it, it is a useful and powerful tool, and should be avoided unless absolutely necessary. I suggest you read up on it here: Trail: The Reflection API (The Java™ Tutorials)

  5. #5
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Why do you think you need to do this? Often, people think they need to, but actually, there's a simpler solution to the problem, often involving polymorphism. What's the problem you're working on? The genuine need for reflection in application code is actually quite rare

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

    Default

    my guess to George's question: because they are coming from a php background. We've seen this time and again.

    To the OP, you're probably going to have to forget everything you've learned about programming in php and learn Java from the ground up as the knowledge you've gained from php doesn't translate well to Java or OOP. Good luck.

  7. #7
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Thanks, Fubarable. PHP encourages some bad habits, eh!

  8. #8
    Gelembjuk is offline Member
    Join Date
    Oct 2008
    Posts
    9
    Rep Power
    0

    Default

    Thanks for all. I am going to apply your advices.

    Quote Originally Posted by georgemc View Post
    Why do you think you need to do this? Often, people think they need to, but actually, there's a simpler solution to the problem, often involving polymorphism. What's the problem you're working on? The genuine need for reflection in application code is actually quite rare
    I work on Maple (Math Software) module that create references for classes coded with Java.
    User just call maple function with 1 argument - classname . And inside this function there is code that call special Java class (Loader) that then create object of that classname , read all methods and retorn to maple. Then maple create fererences for class methods.

    In other words i want create easy way to intergate Java classes to Maple. It will allow to code Math libraries with using of OOP (Maple native language does not support OOP).

    I think it is the only way to create objects dynamically.

  9. #9
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by Gelembjuk View Post
    Thanks for all. I am going to apply your advices.



    I work on Maple (Math Software) module that create references for classes coded with Java.
    User just call maple function with 1 argument - classname . And inside this function there is code that call special Java class (Loader) that then create object of that classname , read all methods and retorn to maple. Then maple create fererences for class methods.

    In other words i want create easy way to intergate Java classes to Maple. It will allow to code Math libraries with using of OOP (Maple native language does not support OOP).

    I think it is the only way to create objects dynamically.
    It depends on the scope of the language, I mean, how many Java classes does it have to deal with? How you answer hints at how to proceed:

    If it's only a handful of already-known classes - like you basically want to wrapper the common java.math classes - that will rarely or never change, I'd just write some interfaces by hand for this.

    If it's a lot of already-known classes that will rarely or never change, I'd take a similar approach to option 1, but write something that generates the source code for them

    If it's any old class at all, and you don't necessarily know about them until runtime, then reflection is probably the way to go. Or you could use something like BeanShell. But either of these two, you should only use if you really do need to, not just to save the hassle of writing the code for either of the above options

Similar Threads

  1. Replies: 3
    Last Post: 10-06-2008, 12:27 AM
  2. How to call methods of different classes
    By adeeb in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 06:08 AM
  3. Object Reflection: Invoking methods
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:14 PM
  4. Replies: 0
    Last Post: 02-17-2008, 08:57 AM
  5. Replies: 0
    Last Post: 01-02-2008, 01:52 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
  •