title: Why doesn't Java understand the class of the object I've placed in this multi-dimensional array?
body: <p>I'm working on a little game but for the sake of example I'll use objects from hypothetical class Student and objects from hypothetical class Books.</p>
  
  <p>I have a multidimensional grid that I want to be populated by either Student objects or Book objects. Maybe it's called </p>
  
  <pre><code>classroom[][] 
  </code></pre>
  
  <p>and has four desks:</p>
  
  <pre><code>[0][0], [0][1], [1][0], and [1][1].
  </code></pre>
  
  <p>I want to be able to place either a Student or a Book (never both), and be able to print </p>
  
  <pre><code>classroom[0][1].someStudentAttribute 
  </code></pre>
  
  <p>if I know it's a student or </p>
  
  <pre><code> classroom[0][1].someBookAttribute 
  </code></pre>
  
  <p>if I know it's a book.</p>
  
  <p>To be clear, it will be obvious when it is a Student or a Book. I won't need a foolproof way to distinguish, that is to say, I'd only be using ".someStudentAttribute" in a situation in the code when it's definitely a student at that desk.</p>
  
  <p>I read somewhere on here I can create a multi-dimensional array that accepts many types of objects by declaring the variable type Object[][] before the name.</p>
  
  <pre><code>Student alice = new Student("alice", 10);
  Student bob = new Student("bob", 11);
  Student charlie = new Student("charlie", 10);
  Book mathBook = new Book("mathematics", 578);
  
  Object[][] classroom = new Object[2][2];
  classroom[0][0] = alice;
  classroom[0][1] = bob;
  classroom[1][0] = charlie;
  classroom[1][1] = mathBook;
  </code></pre>
  
  <p>This returns the proper class (class student)</p>
  
  <pre><code>System.out.println(classroom[0][0].getClass());
  </code></pre>
  
  <p>This also returns the proper class (class book)</p>
  
  <pre><code>System.out.println(classroom[1][1].getClass()):
  </code></pre>
  
  <p>But once I try to print an attribute I get an error:</p>
  
  <pre><code>System.out.println(classroom[0][0].age);
  </code></pre>
  
  <p>The error is:</p>
  
  <pre><code>java: cannot find symbol
  symbol: variable age
  location: java.lang.Object
  </code></pre>
  
  <p>It kind of looks like Java forgot that </p>
  
  <pre><code>classroom[0][0] 
  </code></pre>
  
  <p>belongs to the student class as soon as I ask it for an attribute! If </p>
  
  <pre><code>classroom[0][0] 
  </code></pre>
  
  <p>is not the right way to represent the object in that slot of the array, what is?</p>
  
  <p>Might have something to do with declaring the variable as Object[][], but again the point is to be able to put objects from different classes inside. If I just called it Student[][] I wouldn't be able to put in books.</p>