title: Best way of class design in Java: An example
body: <p>Which is the best design for the below problem description :</p>
  
  <p>A 'Student' has <strong>4 Pens</strong> of colors  <strong>Blue</strong>, <strong>Red</strong>, <strong>Green</strong> and <strong>Orange</strong>.</p>
  
  <p>Here is Pen class-    </p>
  
  <pre><code>Class Pen{
    String color;
    //Getters &amp; setters
    public write(){
      // Write some thing using color
    }
  
  }
  </code></pre>
  
  <p>1st way:</p>
  
  <pre><code>Class Studend{
  //Here pens are stored in a HashMap with its Color as Key
   Map&lt;String,Pen&gt; pens = new HashMap&lt;String,Pen&gt;();
  
   Public Student(Pen p){
     pens.put(p.getColor(),p);//Just an example
   }
   //more code here....
  
  }
  </code></pre>
  
  <p>2nd Way:</p>
  
  <pre><code>Class Student{
    Pen bluePen;
    Pen greenPen;
    Pen redPen;
    Pen orangePen; 
    //Here all the pens are made as class variables.    
  
     //Getters and Setters...
  }
  </code></pre>
  
  <p><strong><em>Note:</em></strong> I need to be able iterate over all the pens and do some common operation such as, </p>
  
  <pre><code>for(Pen pen: pens){ 
    pen.write();
  }
  </code></pre>
  
  <p>which is easier in 1st way.</p>
  
  <p>also, a Pen need to be accessed individually like <code>student.getRedPen();</code> which can be done easily in 2nd way.  But in 1st way I need to do every time as       </p>
  
  <pre><code> if(student.getPens().containsKey("Red")){ 
    Pen redPen = student.getPens().get("Red");
     //some more code here
   }
  </code></pre>
  
  <p>Which is better one? Are there any other better way to achieve this? </p>
  
  <p>Thanks in advance!! </p>