---
title: "stack and heap in java"  
description: "stack and heap in java"  
author: "Anonymous User"  
published: 2015-07-13  
updated: 2015-07-13  
canonical: https://www.mindstick.com/forum/23335/stack-and-heap-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# stack and heap in java

what is [difference between stack and heap](https://answers.mindstick.com/qa/92562/what-is-the-difference-between-stack-and-heap) in [Java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) . Can we create an object in stack [memory](https://www.mindstick.com/blog/300050/what-causes-sudden-memory-loss)? Does object of [private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) is also [stored](https://www.mindstick.com/forum/157561/what-is-the-stored-procedure-create-a-procedure-to-find-the-record-by-stu_id-from-the-student-table) in heap memory??

## Replies

### Reply by Anonymous User

The [stack](https://www.mindstick.com/blog/301746/why-is-stack-overflow-so-important-for-developers) is where local variables are stored. Each time you call a method, all the variables used by that method (including method parameters) get put on top of the stack. When a method returns, those variables are removed from the top of the stack. That way, the top of the stack always contains the variables for the method invocation currently running. Since variables only store primitives and object references, the actual objects need another place to stay. This is the heap. The heap is pretty much just a big piece of memory where objects and their fields are stored. We cannot create objects on the stack. Objects are always created on the heap. Whenever a method is invoked a "Stack frame" is given to the method,where method stores its local variable along with its parameter and it also stores there any information which might be required by a particular jvm. to execute it.Heap is a place where object gets memory as a buffer in which instance variable field of that particular class type is created \
We cannnot mark a class private but we can mark its constructor private. Inner classes can be private, protected and static too.


---

Original Source: https://www.mindstick.com/forum/23335/stack-and-heap-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
