title: How does getClass in Java work
body: <p>Here is what <a href="http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#getClass--" rel="noreferrer">JavaDoc</a> says:</p>
  
  <blockquote>
  <pre><code>public final Class &lt;?&gt; getClass()
  </code></pre>
    
    <p>Returns the runtime class of this <code>Object</code>. The returned <code>Class</code> object is the object that is locked by <code>static synchronized</code> methods of the represented class.<br>
    <strong>The actual result type is <code>Class&lt;? extends |X|&gt;</code> where <code>|X|</code> is the erasure of the static type of the expression on which <code>getClass</code> is called.</strong> For example, no cast is required in this code fragment:</p>
  
  <pre><code>Number n = 0;
  Class&lt;? extends Number&gt; c = n.getClass();
  </code></pre>
    
    <p><strong>Returns:</strong><br>
      The Class object that represents the runtime class of this object.</p>
  </blockquote>
  
  <p>Now , I understand it is a native method , so it is is implemented in platform-dependent code.  But what about the return type of this method.</p>
  
  <pre><code>public final Class&lt;?&gt; getClass()
  </code></pre>
  
  <p>Also , consider the code:</p>
  
  <pre><code>class Dog
  {
      @Override
      public String toString()
      {
          return "cat";
      }
  }
  
  public class Main
  {
      public static void main(String[] args)
      {
          Dog d= new Dog();
          //Class&lt;Dog&gt; dd = new Dog();  Compile time error
          System.out.println(d.getClass());
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>class Dog</p>
  </blockquote>
  
  <p>So, my query lies in :</p>
  
  <ol>
  <li>Return type of this method</li>
  <li>toString method is not called . A similar post on this topic is : 
  <a href="https://stackoverflow.com/questions/16385467/java-getclass-returns-a-class-how-come-i-can-get-a-string-too">Java. getClass() returns a class, how come I can get a string too?</a></li>
  <li>The commented code which otherwise give compile time error.</li>
  </ol>