What is the use of reflection in Java/C# etc

Posted by zengr on Stack Overflow See other posts from Stack Overflow or by zengr
Published on 2010-03-21T20:11:18Z Indexed on 2010/03/21 20:21 UTC
Read the original article Hit count: 215

Filed under:
|

I was just curious, why should we use reflection in the first place?

// Without reflection
Foo foo = new Foo();
foo.hello();

// With reflection
Class cls = Class.forName("Foo");
Object foo = cls.newInstance();
Method method = cls.getMethod("hello", null);
method.invoke(foo, null);

We can simply create an object and call the class's method, but why do the same using forName, newInstance and getMthod functions?

To make everything dynamic?

© Stack Overflow or respective owner

Related posts about reflection

Related posts about java