View RSS Feed

My Java Tips

  1. Reflection example - accessing private data

    by , 05-21-2011 at 05:28 PM (My Java Tips)
    If you think that you cannot access private data members of a class from some other class, then think again. With Reflection, this is possible.

    Using reflection, we can see / view / access a private member, private variable, private method. Some people will not like this with argument that this actually means messing up the laws of encapsulation. I use it for unit testing private methods.

    Example follows:


    Java Code:
    import java.lang.reflect.Field;
    ...
    Tags: reflection Add / Edit Tags
    Categories
    Java SE