Results 1 to 6 of 6
  1. #1
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default how to call class using String

    Hello All,

    I store class name into a string. ie. String classname = "MyClass1"
    how to call it using String ? ( in my case I want to call MyClass1 using classname)

    could any body please help?

    thanks & regards

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    I am sure you do not want/need to do that. What are you trying to achieve?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    Hi

    I store class-name into a table..

    Java Code:
    import java.lang.reflect.*;
    
    String classname = cls1.getClassName(Class1.getModelName());
    Class cp1 = Class.forName(classname);
    
    'then I don't know how to call method name inside the class? :confused:

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,792
    Rep Power
    25

    Default

    Look at the java.lang.reflect package. It has classes and methods for calling methods in classes whose names are determined at execution time.

  5. #5
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    Hi..

    I tried java.lang.reflect. but I am facing with warning below:

    non-varargs call of varargs method with inexact argument type for last parameter;
    cast to java.lang.Class for a varargs call
    cast to java.lang.Class[] for a non-varargs call and to suppress this warning


    Java Code:
    import java.lang.reflect.*;
    
    String classname = "myclass";
    String fcn = "learn1." + classname;
    Class c = Class.forName(fcn);
    Method meth = c.getMethod("mystart", null);
    
    
    ========================================
    public class MyClass1  {
         Class1 cls1 = new Class1();
         public void mystart() {
             cls1.msgBox(null, "HELLO MYSTART", Class1.MESSAGE_INFO);
         }
    }

  6. #6
    newbiejava is offline Senior Member
    Join Date
    Jan 2010
    Posts
    138
    Rep Power
    0

    Default

    just want to share to someone like me, new to java (learning by doing..:))

    Java Code:
    String fcn = "learn." + classname;
    Class c = Class.forName(fcn);
    Method meth = c.getMethod("run");
    Object theObject = c.newInstance();
    meth.invoke(theObject);

Similar Threads

  1. how do i call a class from main class
    By revanger in forum New To Java
    Replies: 7
    Last Post: 07-20-2010, 06:32 PM
  2. Replies: 2
    Last Post: 05-01-2010, 09:59 AM
  3. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 03:33 PM
  4. need help to call another class
    By doha786 in forum New To Java
    Replies: 2
    Last Post: 03-08-2010, 11:40 AM
  5. How can I call method from class in other class??
    By Hisham in forum New To Java
    Replies: 6
    Last Post: 02-14-2010, 04:49 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
  •