What is object lifetime?
Objects are dynamic entities. They are created as required during program execution. When an object is created, it is instantiated, or bound to the class definition. An instantiated member of a class is called an object or an instance. When a new object first comes into existence, a special method called the constructor is automatically invoked by the run-time system. The constructor is responsible for giving an instance its initial state. Once an object has been created, it can receive and send messages.
While an object exists, an object has state and behavior. State is expressed by attributes, and behavior is expressed by the methods associated with the object. State usually reflects changeable attributes of an object. Messages are sent to an object by using its reference in Java or pointer to the object in C++ with the appropriate method name.
Once an object is no longer needed, it can be destroyed. Objects commonly go out of existence as a normal part of program execution. Some programming languages (e.g.,C++) allow for the explicit destruction of objects using the delete operator. In case of Java an object ceases to exist whenever it no longer has any references to it from other objects, at which point it may be garbage collected by the Java run-time system.
