I looked up the method class and thought that it might be useful, but unfortunately Method objects must be public to be invoked.

I'm just curious of when I'd actually really need to use the Method class. Maybe to create an array that used a type of algorithm to fire different methods?

Sorry if this is too vague. I'm just really curious.

Recommended Answers

All 3 Replies

Reflection. you need it when you want to invoke a method by reflection. A method does not have to be public to be invoked. However, you will need to know how to get it. (Class.getMethod() will not work, it only works for public methods; Class.getDeclaredMethod() will work)

I'm not quite familiar with java.lang.Reflect , honestly I've only dabbled with that class through experimentation.

I.E., when it came to creating generic-based methods.

What is the primary usage of the Reflect package?

I'm not quite familiar with java.lang.Reflect , honestly I've only dabbled with that class through experimentation.

I.E., when it came to creating generic-based methods.

What is the primary usage of the Reflect package?

You use the reflection classes when the classes and/or methods you want to use are not known at compile time. For example, you can specify class and method names as strings based on some user input, and instantiate the classes and execute the methods from there.

I use it for report generation, when the report layout depends on what state the user is in (retrieved from a database). The basic data is the same, so each state's version is just a subclass of a more generic report class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.