title: Can an object be stored on the stack instead of the heap in java?
body: <p>Can an object be stored on the stack instead of the heap?</p>
  <p>I recently gone through this blog
  <a href="http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/" rel="noreferrer">http://www.programmerinterview.com/index.php/data-structures/difference-between-stack-and-heap/</a></p>
  <blockquote>
  <p>Can an object be stored on the stack instead of the heap?</p>
  <p>Yes, an object can be stored on the stack. If you create an object inside a function without using the “new” operator then this will create and store the object on the stack, and not on the heap. Suppose we have a C++ class called Member, for which we want to create an object. We also have a function called somefunction( ). Here is what the code would look like:</p>
  </blockquote>
  <p>they suggesting that Objects can stored in Heap. and it was confusing.</p>
  <p>I thought that,</p>
  <ol>
  <li><p>All objects in Java are stored on the heap.<br />
  whether it is created by<br />
  a. <code>new Keyword</code><br />
  b. Using <code>Class.forName().</code><br />
  c. Using <code>clone().</code><br />
  d. Using <code>newInstance()</code> method<br />
  e. Using Object Deserialization.</p>
  </li>
  <li><p>Methods,threads and variables are in stack.</p>
  </li>
  <li><p>Class variables(Static variables) are stored as part of the Class object associated with that class. This Class object can only be created by JVM and is stored in permanent generation.</p>
  </li>
  </ol>
  <p>Please do correct me if i am wrong.</p>
  <p>Now my doubt is whether objects can reside or stored in stack in any form.</p>
  <p>Thank you.</p>