Where in memory are the objects located when they are created in a static block?

If I create a static block and create an object there, say about some other class, will the object be created on the heap or on the stack?

class Hello {
   static {
       Abc abcObject=new Abc();
   }
   // Other Code...
} 
+5
source share
2 answers

An object is created on the heap, but an object reference is on the stack.

The variable abcObjectyou created is on the stack. This contains the memory address on the heap where the object is stored new Abc().

+4
source

Objects are always on the heap regardless of static (or) non-static.

Links will be on the stack.

+7
source

All Articles