title: Get and Set methods in Java. How do they work?
body: <p>I'm trying to get a better understanding than I've been able to so far...it's to help me with class, but not with any specific homework assignment; I'm looking for more conceptual information that I've not found in my textbook or in the tutorials. On a purely theoretical level, I get why encapsulation is powerful for OO programming, but need a better understanding of what, specifically is going on. I keep confusing myself about the path through which data flows, and wonder where we're getting and setting data FROM. Hopefully this is not the exception to the "there are no stupid questions" rule.</p>
  
  <p>Consider:</p>
  
  <pre><code> public class Employee
   {
      private int empNum;
      public int getEmpNum()
      {   return empNum;   }
      public void setEmpNum(int Emp)
      {   empNum = emp;   }
   }
  </code></pre>
  
  <p>My interpretation of this: Using <code>int Emp</code> passed from somewhere, we set the declared empNum variable in the <code>set...</code> method. We then <code>get...</code> that variable and return it to the calling method. Is that a good understanding?</p>
  
  <p>Further Consider:</p>
  
  <pre><code>import.javax.*
  public class CreateEmp
  {
      public static void main(String[] args)
      {
           int ssn;
           ssn = JOptionPane.showInputDialog(null,"enter ssn");
           Employee newEmp = new Employee(123456789);
           JOptionPane.showMessageDialog(null,"Employee ID " + newEmp.getEmpNum);
       }
   }
  </code></pre>
  
  <p>With this class, I don't need to explicitly <code>set</code> a variable because declaring <code>new Employee(123456789)</code> automagically sends that value to the <code>set...</code> method. The <code>return</code> in the Employee method returns the resulting value to the calling method and makes that variable available, in this case to my dialog box output. Do I have a handle on that?</p>
  
  <p>Here's where I get confused. With my understanding as noted above--which may be off--I can never decide the best place to put logic. For instance: I would place the "enter employee number" dialog box in the <code>Employee</code> class, with a condition to run ONLY if the parameter passed to the method was null. In my mind, as I understand the data flow, the purpose of the class is to get an employee number. If one is not supplied as a default, one needs to be created. I'm not sure if this thinking is best practices, or if I'm setting myself up for trouble later.</p>
  
  <p>I'd love to hear some thoughts about whether or not I'm properly understanding what's happening.</p>