Search
Calendar
June 2011
S M T W T F S
« May   Jul »
 1234
567891011
12131415161718
19202122232425
2627282930  
Archives

Archive for June 28th, 2011

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);