Skip to content
Sharp

Reflection API · interview question

If you use Method.invoke() to call a private method, what must you do first?

Multiple choiceHardchapter-16-curatedpremiumreflection-apimethod-invoke
Choose one answer
Answer and explanation

Correct answer

C. Set the method to accessible

Why

In order to call a private method using reflection, you must first call setAccessible(true) on the Method object to bypass Java's access control checks.

Test yourself on Reflection API

A free test with instant, explained results. No account needed.

Start free test
All Reflection API questions →