• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

how can i change String 'value to class???

eg:
  String s="StringBuffer";

i want this.
   StringBuffer sb=new StringBuffer();

this StringBuffer class i want get the s'value ("StringBuffer"),and change "StringBuffer" String to StringBuffer class.

how can i do it???
0
hgbdelphi
Asked:
hgbdelphi
2 Solutions
 
CEHJCommented:
Class c = Class.forName("java.lang.StringBuffer");
0
 
COBOLdinosaurCommented:
For reusability would you do it with two arguments with a package an a class so you could do:

 Class c = Class.forName(packge+"."+cls);

Cd&
0
 
hgbdelphiAuthor Commented:
but ,this get the class Object,i want to get the StringBuffer or another my define  class and do another method from StringBuffer or another my define  class
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
neonlinesCommented:
 String s="StringBuffer";
 StringBuffer sb=new StringBuffer(s);

System.out.println(sb.toString());
0
 
CEHJCommented:
>> but ,this get the class Object,

That's what you said you wanted:

>>how can i change String 'value to class???

You can create an instance thus:

            try {
                   Class c = Class.forName("java.lang.StringBuffer");
                   StringBuffer sb = (StringBuffer)c.newInstance();
                   System.out.println(sb);
            }
            catch(Exception e) {
                  e.printStackTrace();
            }
0
 
CEHJCommented:
I meant to post this for something more meaningful:

System.out.println(sb.getClass());
0
 
hgbdelphiAuthor Commented:
i want ithis,

String s="A"
A a=new A();
a.doSomthing()

String s="B"
B b=new B();
b.doSomhting1();


0
 
CEHJCommented:
Something like this maybe?

                  String s="A"
                  Class c = Class.forName(s);
                  A a = (A)c.newInstance();
0
 
hgbdelphiAuthor Commented:
but how can  i get A a=(A)c.newInstance();
now it only s a=(s)c.newInstance()

0
 
hgbdelphiAuthor Commented:
how can i do this

 "StringBuffer" s=new "StringBuffer"();
to
  StringBuffer s=new StringBuffer();

0
 
CEHJCommented:
In order to do that, you'll need to delve deeper into reflection:

http://javaalmanac.com/egs/java.lang.reflect/Methods.html?l=rel
0
 
achilkaCommented:
you can do this

String s = "java.lang.StringBuffer";
        Class myclass = Class.forName(s);
        Object my = myclass.newInstance();
and
also refer to CEHJ link above to get methods

if you know for sure that you always have StringBuffer as class name for s
the you could use StringBuffer = myclass.newInstance().
or else you are expecting a pre-defined classes then use
if(my instanceof CLASS)
//DO TYPE CAST
else if(my instanceof CLASS2)
//DO TYPE CAST

0
 
hgbdelphiAuthor Commented:
i want this

if
string s="MyClass1"

i change

"MyClass1" s=new "MyClass1"();

to

MyClass1 s=new MyClass1();
s.doMyClass1SomeMethod();
s.doAnotherMethod();
...

and if String s="MyClass2"

i can change
"MyClass2" s=new "MyClass2"()
to
MyClass2 s=new MyClass2();
s.doMyClass2SomeMethod();
s.doAnotherMethod();
.....


and it can not use like this

interface AAA{
public void doSomething();
}

AAA a = (AAA)Class.forName("SomeClass").newInstance();
a.doSomething();

because AAA or BBB or CCC and another my define class with differenet method,and i can not define all method in Interface.

thanks!
0
 
aozarovCommented:
String classNm = "ClassName";
String methodNm = "methodName";
Class clazz = Class.forName(classNm);
Method method = clazz.getMethod(methodNm, new Class[] {}); // get method with no arguments;
Object obj = clazz.newInstance();
method.invoke(obj, new Object[]{}); // invoke the method on that specific intance
0
 
hgbdelphiAuthor Commented:
hi,aozarov

  thanks,and can tell me Method in what package?
0
 
hgbdelphiAuthor Commented:
thanks very much,it can work well,this is my demo code.
import java.lang.reflect.Method;

public class TestMain
{
      public TestMain(){
      
        System.out.println("TestMain();");
      }

      public static void main(String[] args){
      String classNm = "Test1";
    String methodNm = "showTest";
      try{
    Class clazz = Class.forName(classNm);
    Method method = clazz.getMethod(methodNm, new Class[] {}); // get method with no arguments;
    Object obj = clazz.newInstance();
    method.invoke(obj, new Object[]{}); // invoke the method on that specific intance
      }catch(Exception e){
        System.out.println(e.getMessage());
      }
      }
}


---------------

public class Test1
{


      public Test1(){
        System.out.println("Test1");
      }



      public void showTest(){
        System.out.println("showTest();");
      
      }
}

and on control it print

Test1
showTest()


thanks all!
0
 
hgbdelphiAuthor Commented:
if in Test1 have a method
  public void getShowTest2(String s){
   System.out.println(s);  
}


in TestMain,how can i invoke getShowTest2(String s) with argument???



thanks.
0
 
hgbdelphiAuthor Commented:
thanks i work it

import java.lang.reflect.*;

public class TestMain
{
      public TestMain(){
      
        System.out.println("TestMain();");
      }

      public static void main(String[] args){
      String classNm = "Test1";
    String methodNm = "showTest2";
      try{
    Class clazz = Class.forName(classNm);
    Method method = clazz.getMethod(methodNm, new Class[] {Class.forName("java.lang.String")}); // get method with no arguments;
    Object obj = clazz.newInstance();
    method.invoke(obj, new Object[]{"xxxx"}); // invoke the method on that specific intance
      }catch(Exception e){
        System.out.println(e.getMessage());
      }
      }
}
0
 
hgbdelphiAuthor Commented:
thanks very much!
now i can work it .
0
 
CEHJCommented:
:-)

Did you read that link i posted before, together with the link underneath:

 Invoking a Method Using a Method Object

?
0
 
hgbdelphiAuthor Commented:
hi,CEHJ,
  i read the link,the link tells me to Getting the Methods of a Class Object,but i not use it,in above i can do it :)

thanks for your help!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now