How to Get a Private Field of an Object using Reflection in Java

A useful function that allows to get access to a private field of an object.

public static Object getFieldAsPublic(Object obj, String field) 
       throws SecurityException, NoSuchFieldException, 
              IllegalArgumentException, IllegalAccessException
{
    Field f = obj.getClass().getDeclaredField(field);
    f.setAccessible(true);
    return f.get(obj);
}