Deconstruct versus Finalization
When you move from C++ to Java, one of the more subtle, yet important issues you
will face is the difference between a C++ constructor a Java finalize() method. Although similar in many respects, their actual operation is distinctively different. Let’s. begin by reviewing the purpose and effect of n c++ destruction and the Java finalize( ) method. In C++, when an object goes out of scope, it is destroyed, Just prior to its destruction,its destruct or function is called (if it has one). This is a hard-and-fast rule. There a re no exceptions. Let’s look more closely at each part of this rule:
*Every object is destroyed when it goes out of scope. Thus, if you declare a local object inside a function; when that function returns, that local object is automatically destroyed. The same goes for function parameters and for objects returned by function.
*Just before destruction, the object’s destruct is called. This happens immediately, and before any other program statements will execute. Thus, “C++ deconstruct will always execute in a deterministic fashion. You can always know when and where a deconstruct will be executed.