6.5 Creating and Destroying Objects
If a program creates objects via the new keyword, the memory management of the runtime system reserves memory on the system heap. Then, the object states are initialized. If the object is no longer referenced, the automatic garbage collector cleans up at certain intervals and returns the memory to the runtime system.
6.5.1 Writing Constructors
When a program creates a new object via new , a constructor is automatically called for initialization. Every class must have a constructor in Java. Either the compiler automatically creates a constructor, or you’ll create your own. With your own constructor, you can set up an object with an initial, user-defined state after its creation. This approach may ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access