As I understand, in Java, stack memory holds primitives and method invocations and heap memory is used to store objects.
Suppose I have a class
class A {
int a ;
String b;
//getters and setters
}
Where will the primitive
ain classAbe stored?Why does heap memory exist at all? Why can't we store everything on the stack?
When the object gets garbage collected, is the stack associated with the objected destroyed?