title: Setting values to an Array of objects
body: <p>I'm revising my Java book to make sure i have solid understanding of objects and Java basics in general. I stumbled upon this piece of code from the book i was reading  <a href="https://rads.stackoverflow.com/amzn/click/com/0596009208" rel="nofollow noreferrer" rel="nofollow noreferrer">Head First: Java 2nd edition (2005)</a></p>
  
  <pre><code> class Book {   
  
       String title;
       String author; 
  
   }
  
   class Main {
  
      public static void main(String args[]) {    
  
          int x = 0;      
  
          Book[] myBooks = new Book[3];
          myBooks[0] = new Book();
          myBooks[1] = new Book();
          myBooks[2] = new Book();
          myBooks[0].title = "Example title xx";
          myBooks[1].title = "Example title cc";
          myBooks[2].title = "Example title yy";
          myBooks[0].author = "Example author xx";
          myBooks[1].author = "Example author cc";
          myBooks[2].author = "Example author yy";    
  
          while (x &lt; 3) {     
  
              System.out.print(myBooks[x].title);
              System.out.print(", author ");
              System.out.println(myBooks[x].author);
              x = x + 1;  
  
          }       
      }   
  }
  </code></pre>
  
  <p>I don't quite understand the syntax of <code>myBooks[0].title = "Example title xx"</code>
  I admit i'm not quite experienced with arrays and how they work yet, but isn't it a better practice to loop through the array and set all the object fields with setter methods?</p>
  
  <p><strong>What i think is the case here</strong></p>
  
  <p>From my limited understanding, This particular method of assigning values to those fields is related to the scope of those two classes. The same way you'd use names of the static methods instead of first creating object of their respective classes, but with static variables instead.</p>
  
  <p>It seems very trivial, but it's very important for me to understand and grasp the idea. I hope you can clear it up for me.</p>