Object lifetime
📖 Scenario: Imagine you are managing a small library system. Each book is represented as an object. You want to understand when these book objects are created and when they are no longer needed (destroyed).
🎯 Goal: You will create a simple Java program that shows the creation and destruction of book objects by printing messages when objects are created and when they are about to be destroyed by the garbage collector.
📋 What You'll Learn
Create a
Book class with a constructor that prints a creation messageOverride the
finalize() method in Book to print a destruction messageCreate multiple
Book objects in the main methodRemove references to some
Book objects and call the garbage collectorPrint messages to observe object lifetime
💡 Why This Matters
🌍 Real World
Understanding object lifetime helps manage memory in programs and avoid memory leaks.
💼 Career
Java developers must know how objects are created and cleaned up to write efficient and reliable applications.
Progress0 / 4 steps
