Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible in Java to access private fields via reflection [duplicate]

Is it possible in Java to access private field str via reflection? For example to get value of this field.

class Test {    private String str;    public void setStr(String value)    {       str = value;    } } 
like image 470
Volodymyr Bezuglyy Avatar asked Oct 12 '09 16:10

Volodymyr Bezuglyy


People also ask

Can java reflection API access private fields?

If we want to access Private Field and method using Reflection we just need to call setAccessible(true) on the field or method object which you want to access. Class. getDeclaredField(String fieldName) or Class. getDeclaredFields() can be used to get private fields.

Can we access private methods using reflection?

You can access the private methods of a class using java reflection package.

Why we should not use reflection in java?

It's very bad because it ties your UI to your method names, which should be completely unrelated. Making an seemingly innocent change later on can have unexpected disastrous consequences. Using reflection is not a bad practice. Doing this sort of thing is a bad practice.

Can you access private variables in the same class?

This is perfectly legal. Objects of the same type have access to one another's private variables. This is because access restrictions apply at the class or type level (all instances of a class) rather than at the object level (this particular instance of a class).


2 Answers

Yes, it absolutely is - assuming you've got the appropriate security permissions. Use Field.setAccessible(true) first if you're accessing it from a different class.

import java.lang.reflect.*;  class Other {     private String str;     public void setStr(String value)     {         str = value;     } }  class Test {     public static void main(String[] args)         // Just for the ease of a throwaway test. Don't         // do this normally!         throws Exception     {         Other t = new Other();         t.setStr("hi");         Field field = Other.class.getDeclaredField("str");         field.setAccessible(true);         Object value = field.get(t);         System.out.println(value);     } } 

And no, you shouldn't normally do this... it's subverting the intentions of the original author of the class. For example, there may well be validation applied in any situation where the field can normally be set, or other fields may be changed at the same time. You're effectively violating the intended level of encapsulation.

like image 89
Jon Skeet Avatar answered Sep 28 '22 13:09

Jon Skeet


Yes.

  Field f = Test.class.getDeclaredField("str");   f.setAccessible(true);//Very important, this allows the setting to work.   String value = (String) f.get(object); 

Then you use the field object to get the value on an instance of the class.

Note that get method is often confusing for people. You have the field, but you don't have an instance of the object. You have to pass that to the get method

like image 35
Yishai Avatar answered Sep 28 '22 12:09

Yishai