is a variable with a private access modifier .. not visible to the new object created using that class definition?
<p>I wanted to know whether a private access modifier before a variable in a class definition  makes it inaccessible to the object created?</p>

<p>Simply putt, are the values of private attribute variables loaded in the heap memory when a new object is created using that class?</p>

<p>For example: suppose I create a class (lets call it <em>CLASS1</em>) which has an attribute variable called <em>private int p1=10;</em> and i make a method <code>public void Updatep1(int a){
p1=a;}</code></p>

<p>Now if I create an object <code>CLASS1 object1=new CLASS1</code>
in the main method and then try to print the value of p1, I get an error saying that it is inaccessible as it's a non-static variable.. and hence inaccessible!</p>

<p>Please look at the following example:</p>

<pre><code>public class DateThree {

  private int day;
  private int month;
  private int year;

  public void setDay(int d) {
    day = d;
  }

  public int getDay() {
    return day;
  }

  public void setMonth(int m) {
    month = m;
  }

  public int getMonth() {
          return month;
  }

  public void setYear(int y) {
    year = y;
  }

  public int getYear() {
    return year;
  }

  public int calcDaysInMonth() {

    switch(month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
      return 31;
    case 2:
      return 28;
    case 4:
    case 6:
    case 9:
    case 11:
      return 30;
    default:
      System.out.println("Invalid month.");
      return 0;
    }
  }
</code></pre>

<p>If I call the above class from another class..</p>

<pre><code> public class DateThreeTest {

  public static void main(String args[]) {

    DateThree d = new DateThree();
    int days = d.calcDaysInMonth();
    System.out.println("Days in Month:" + days);
  }
 }
</code></pre>

<p>But as per theory, this should not work, as <em>month</em> is private and hence inaccessible to the new object, and hence the <em>d.CalcDaysInMonth</em> should give an error as it requires a private method <em>month</em> to function, which is not there in the new file and is neither accessible to the new method, yet it does not give an error, it works!</p>

<p>So my question is: from where does the object in the DateThreeTest (namely <code>d</code>) get the month variable's value?</p>
