object created within a class gets access to private fields.. and objects created in a different class don't why?
<p>First of all i created a class called <code>PrivateShirt1</code> and called a private field name after creating an object of the class within the same class file, and it worked.</p>

<pre><code>public class PrivateShirt1{
   private String name;

   public static void main(String args[]){               
       PrivateShirt1 s1=new PrivateShirt1();
       s1.name="hi";

       System.out.println(s1.name);    
   }
}
</code></pre>

<p>next i created a separate file called <code>PrivateShirt2</code> in which i put the main method of <code>PrivateShirt1</code> and performed the function of  compiling and calling it. After the modifications the files <code>PrivateShirt1</code> and <code>PrivateShirt2</code> look like this:</p>

<pre><code>public class PrivateShirt2{ 

  public static void main(String args[]){               
     PrivateShirt1 s1=new PrivateShirt1();
     s1.name="hi";

     System.out.println(s1.name);    
  }


public class PrivateShirt1 {
   private String name;    
}
</code></pre>

<p>and when i compiled the <code>PrivateShirt2</code> file, it gave an error that the attribute i was trying to call is private.</p>

<p>but then why did this not happen in the previous example? I mean, objects were created in both the cases and hence the rules should be equal for both, right? so why then this partial treatment? could anyone elaborate?</p>
