lukuluku
lukuluku

Reputation: 4404

Dynamically load a Class and invoke a method in Java

Lets say i want to dynamically load a class in java and call it's start() (has no params) method:

Class<?> c = Class.forName("AbuseMe");
c.getMethod("start").invoke(c.newInstance());

Would this be a good/safe way to do it?

Upvotes: 5

Views: 13345

Answers (1)

ᴇʟᴇvᴀтᴇ
ᴇʟᴇvᴀтᴇ

Reputation: 12751

Looks good to me.

If you're doing a lot of reflection-related code you might look at Apache Beanutils or Apache OGNL or something similar.

Upvotes: 1

Related Questions