• Post Reply Bookmark Topic Watch Topic
  • New Topic

objects creation  RSS feed

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i know that in Java i can create an object without knowing the
implementation of the object, and later i give him the object type
as a String, and i can call methods in an object the same way, but i don't
know the exact code, does anyone know how to do it, hope i was clear in
my question.
Thanks, Omar
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean reflection & stuff?

Object o = Class.forName("test.Foo").newInstance();

Method m = o.getClass().getMethod("test", new Class[] {String.class}); // test.Foo.test(String)

Object result = m.invoke(o, new Object[] {"hello world"});
 
omar bili
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that is exactly what i wanted, thanks very much
Omar
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!