Search
Calendar
April 2024
S M T W T F S
« Sep    
 123456
78910111213
14151617181920
21222324252627
282930  
Your widget title
Archives

PostHeaderIcon How to access non-visible fields in Java?

How to access a non-acccessible field (either protected, package-protected or private) of an object in Java?

For instance, you would like to access the field threads of ThreadGroup:

ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
final Field privateThreadsField;
privateThreadsField = ThreadGroup.class.getDeclaredField("threads");
privateThreadsField.setAccessible(true);
threads = (Thread[]) privateThreadsField.get(threadGroup);

Leave a Reply