Forums Register Login

stack and heap in java

+Pie Number of slices to send: Send
what is difference between stack and heap in Java . Can we create an object in stack memory?
Does object of private class is also stored in heap memory??
1
+Pie Number of slices to send: Send
The stack 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.

Whether classes are private or not does not influence how they are stored at all.
+Pie Number of slices to send: Send
See Call stack on Wikipedia to understand what the stack is and how it works.

You cannot create objects on the stack. Objects are always created on the heap*.

* Newer versions of Java have a special optimization, escape analysis, so it is strictly speaking no longer true that objects are always allocated on the heap. However, this is an advanced implementation detail that you don't need to know about. You still cannot explicitly allocate an object on the stack instead of on the heap.
+Pie Number of slices to send: Send
"stack" and "heap" are overloaded terms. They are both DATA STRUCTURES as well as KINDS OF MEMORY. The way you word your post, we assume you are talking about the latter, but your subject line is unclear.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Also I want to add one more thing.. a class cannot be marked private. A class can only be marked public,abstract or final.
+Pie Number of slices to send: Send
ya but we can make its constructor private..
So problem solved
+Pie Number of slices to send: Send
 

Ashwin Rao wrote:Also I want to add one more thing.. a class cannot be marked private. A class can only be marked public,abstract or final.



Inner classes can be private, protected and static too.
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 637 times.
Similar Threads
Why declare variables static
static vs instance methods
garbage collection heap?
Java array and String problem
dif. between class and instance data members
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:54:51.