Is it possible to set private property via reflection?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Java, reflection can be used to access private members, but the exact answer depends on what you mean by "private property." Java does not have first-class properties in the C# sense. It has fields and methods, so reflection can set a private field directly or invoke a private setter if access restrictions are opened.
Setting a Private Field
The most direct case is a private field.
This prints Ada because reflection bypassed normal access checks.
Setting a Private Property via a Setter Method
If by property you mean a private setter method, reflection can invoke that too.
So yes, private state can be changed reflectively if the runtime allows access to be opened.
Why This Works
Reflection can inspect class metadata and request access to non-public members using setAccessible(true). Historically, that was enough in many Java environments.
But this is not a free-for-all. Modern Java adds stronger encapsulation, especially with the module system.
Java 9 and Later: Modules Change the Story
Since Java 9, strong encapsulation means setAccessible(true) may not be enough if the target package is not open to your module. In those cases you may see InaccessibleObjectException.
So the modern answer is:
- yes, reflection can set private state
- no, it is not guaranteed in every module boundary without the right opens configuration
If the code is in your own application modules, you can often solve this by opening packages appropriately. If it is inside JDK internals or a closed module, reflective access may be blocked.
Final Fields Are a Special Case
Private final fields are more complicated. Reflection may allow some forms of access, but changing final fields reliably is not a normal or safe programming technique. Even if you manage to mutate one in a particular environment, optimization and object-model assumptions can make the behavior surprising.
Treat final reflective mutation as an edge-case hack, not as a regular design tool.
When Reflection Is Reasonable
Legitimate use cases include:
- testing legacy code
- framework internals such as serializers or dependency injection containers
- migration tools or adapters
- debugging utilities
Even in these cases, reflective private access is usually a last resort rather than the best API design.
Why It Is Often a Design Smell
If ordinary application code repeatedly needs to set private fields reflectively, the class design may be fighting the use case.
Better options may be:
- add a public or package-private method
- use a builder or constructor parameter
- expose a testing seam
- redesign the object so state is initialized explicitly
Reflection is powerful, but power and maintainability are not the same thing.
Security and Maintainability Concerns
Bypassing access control weakens encapsulation. It can also break when:
- class names change
- field names are renamed
- module boundaries become stricter
- security or runtime configuration changes
This makes reflection-based private mutation more fragile than normal API usage.
Common Pitfalls
- Saying "property" in Java when the real target is a private field or private setter.
- Assuming
setAccessible(true)always works in Java 9 and later module-aware environments. - Using reflection in ordinary application code where a better API would be clearer.
- Trying to mutate private
finalfields as if they were normal mutable fields. - Forgetting that reflective code is brittle under refactoring because member names are hardcoded.
Summary
- Yes, Java reflection can set private fields or invoke private setters when access can be opened.
- The common mechanism is
getDeclaredFieldorgetDeclaredMethodplussetAccessible(true). - Strong encapsulation in modern Java can block reflective access across module boundaries.
- Reflective access to private state is useful in some frameworks and tests, but it is often a sign that the public API is insufficient.
- Use reflection sparingly because it bypasses encapsulation and creates brittle code.
Related reading
- is it safe to keep database connections open for long time
- Is there a unique Android device ID?
- Is there a way to create a token for a normal user in Kubernetes?
- Is there a way to get the source code from an APK file?
- Is it possible to use Java 8 for Android development?
- Is it possible to use Java 8 for Android development?
- Is there a way to get the source code from an APK file?
- Is there an S3 policy for limiting access to only see/access one bucket?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.