title: Dosen't Reflection API break the very purpose of Data encapsulation?
body: <p>Very recently I came across the Reflection API and to my surprise we can access and even alter the private variables.I tried the following code</p>
  
  <pre><code>import java.lang.reflect.Field;
  
  public class SomeClass{
      private String name = "John";
  }
  
  public class Test{
      public static void main(String args[]) throws Exception {
          SomeClass myClass = new SomeClass();
  
          Field fs = myClass.getClass().getDeclaredField("name");
          fs.setAccessible(true);
  
          System.out.println("Variable is " + fs.getName() + " and value is "
                  + fs.get(myClass));
  
          fs.set(myClass, "Sam");
          System.out.println("Variable is " + fs.getName() + " and value is "
                  + fs.get(myClass));
      }
  }
  </code></pre>
  
  <p>and I got the following output.</p>
  
  <pre><code>Variable is name and value is John
  Variable is name and value is Sam
  </code></pre>
  
  <p>We say Java is an Object oriented language and it's main features are Data Encapsulation, Inheritance, Polymorphism.. etc. Isn't the reflection API changing the very purpose of Data Encapsulation? Why do we have to use Reflection API? I read in some sites that it can be used for testing purpose but according to me modules are tested and that can be done easily using JUnit test cases. So can anyone explain why do we have such a hack?</p>