Ceelos
Ceelos

Reputation: 1126

How to obtain Declared Methods in any given java class

I understand that the following exists:

Class clazz = Class.forName("SomeClassWithinSameDirectory);

Method methods[] = clazz.getDeclaredMethods();

but, from what I've tried, this only works given that the class I'm using is within the same directory as the class that has my main method.

Does Java have a simple way of working around this problem? I've been googling a bit but have come up with nothing. Someone guide me in the right direction? Thanks in advance.

Upvotes: 0

Views: 50

Answers (1)

Mena
Mena

Reputation: 48404

Actually you can just use Class clazz = Class.forName("my.package.SomeClass"); and reflection will attempt to load that class, or throw a ClassNotFoundException.

Also take a look at the more complex alternate forName signature, which lets you choose whether the class should be initialized and with which ClassLoader to do so.

Upvotes: 2

Related Questions