First understand the reasons for not being able to allocate an object to stack. Take advantage and try to understand the functioning of stack and of heap, if you still have doubts.
So the normal of Java is just to put the primitive types in stack (they may be in the heap also), since it meets the criteria of not escaping the scope of the method, be guaranteed very small, etc. (see the criteria in link above). It is predicted in future versions that the programmer will be able to create his own types with value semantics, as are the primitives, as are already occurs in C#.
What the JVM can do with the classes that, in theory, are always allocated in the heap is to optimize allocation and put it in the stack when it can determine that this is possible. The main reasons for meeting this are subject to verification of the actual implementation of the JVM, but we can infer a few things that are the general criteria governing the allocation of memory in any technology.
- The main and easy to discover is that the object needs to be small. It can be said that the JVM always knows the size of the object when going to instantiate. I just can’t guarantee that it’s always viable to verify this, I believe it is, but if for some reason there’s a case that isn’t, size indetermination will prevent optimization. In general any normal class has sufficient size, arrays even if used in composition to a class is that they may have difficulty.
- The text speaks clearly that it is made a exhaust analysis to make sure that the object does not leave the domains of the method in any way: by direct return, by attaching to any other object that can be returned, referenced by the argument passed to the method, including there the hidden parameter
this
giving access to the members of the panel.
If you cannot prove that the object does not escape, it is preferable to keep in the heap. Even if the object is small enough to make a copy, the expected external semantics does not allow you to change the reference by the copied value.
In some cases even not escaping may not compensate put in stack because it may impair the reference location of the objects making the composition.
- It is possible that there are other specific criteria that I would not know. Any minimum impediment will avoid optimization. The JVM will certainly prefer a false negative that hinders a possible optimization than a false positive that creates a failure or instability to the JVM.
It is not guaranteed that the object goes to the stack
Like all optimization, do not count on it, it will only occur to help. If you depend on it for something you are expecting more than you should. What can work one way at a time may no longer occur another, either by a change of JVM, or by a change in your code. Not always a change makes clear what may occur.
Optimization depends on implementation, it is not part of the specification that it should occur. So it depends on the JVM vendor, the version you’re using and the settings applied.
remember that there are several implementations of JVM, and these criteria may vary between them.
– Vinicius Zaramella
@Viniciuszaramella yes, I improved the answer
– Maniero