Where in memory are objects located when they are created within a static block?
-
30-06-2021 - |
Frage
If I create a static block and create an Object there, say of some other class, will the object be created on the heap or on the stack?
class Hello {
static {
Abc abcObject=new Abc();
}
// Other Code...
}
Lösung
The object is created in the heap, but the reference to the object is in the stack.
The variable abcObject
which you created is located in the stack. This contains a memory address within the heap where the new Abc()
object is stored.
Andere Tipps
Objects are always on heap irrespective of static (or) non-static .
References will be on stack.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow