Skip to content
Sharp

Reflection API · interview question

How would you use MethodHandles.Lookup to access a private field in a class?

Multiple choiceHardchapter-16-curatedpremiumreflection-apimethodhandles
Choose one answer
Answer and explanation

Correct answer

A. Use findVarHandle with a trusted Lookup.

Why

findVarHandle is used to create a VarHandle for instance fields, including private fields, when using a trusted Lookup object that has the necessary permissions set.

Test yourself on Reflection API

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

Start free test
All Reflection API questions →