answersLogoWhite

0


Best Answer

I agree with @Hilmar. You can never predict when exactly the object will be destroyed. But, you can definitely predict when an object is ready to be destroyed.

Difference between the two can be easily explained using an example (I'm using JAVA here):

Employee emp = new Employee("Dexter");

This statement creates an object of Employee (with name Dexter) which resides in the heap.

As long as you are using the reference "emp" for this object , the object will stay there. Its indestructible. Garbage Collector cannot even touch it.

Now lets say, after a few lines of code, you are executing:

emp = new Employee("RITA");

This creates a new object of Employee (with name RITA), but notice that the reference "emp" no longer points to our old object with the name Dexter. Hence the object of Employee with the name Dexter has no references associated with it.

And this makes it eligible for the Garbage Collection, and whenever the next round of Garbage Collection happens, the mighty GarbageCollector will not leave this chance to eat up the Employee (with the name Dexter).

User Avatar

Wiki User

8y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

8y ago

I assume you are talking about programming languages. It really depends on the specific language. In some languages, as soon as there are no more references to the object. In languages with a garbage collector (such as Java), you don't know in advance when objects will get destroyed - objects that are no longer accessible will be removed whenever the garbage collector removes them - and if the program is very busy, and there is no shortage of storage space, that might not happen while the program is running.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When does an object get destroyed in programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is an essential of Object Oriented Programming?

The 3 essential concepts of Object Oriented Programming are:InheritanceEncapsulation &Polymorphism


What is object based programming language?

Object-based programming language is a language that supports all the features of object oriented programming features like classes,object,encapsulation ,abstraction,polymorphism etc except inheritence.


Full form of oops?

The full form of OOP is Object-Oriented Programming.


What actually mean by object oriented programming Is C plus plus is a object oriented programming?

Just eat a watermellon!


Can we create an array of objects for a class having default constructor Justify your answer?

See example code below. #include <iostream> class x { private: // Members. static int counter; int data; public: // Default constructor and destructor. x():data(++counter){printf("Object %d created!\n",data);} ~x(){printf("Object %d destroyed!\n",data);} // other members omitted for brevity... }; // Initialise static counter. int x::counter=0; int main() { // Instantiate an array of 10 objects. x arr[10]; // .. do some work with the array .. return( 0 ); // The array falls from scope, destroying the objects. } Example output: Object 1 created! Object 2 created! Object 3 created! Object 4 created! Object 5 created! Object 6 created! Object 7 created! Object 8 created! Object 9 created! Object 10 created! Object 10 destroyed! Object 9 destroyed! Object 8 destroyed! Object 7 destroyed! Object 6 destroyed! Object 5 destroyed! Object 4 destroyed! Object 3 destroyed! Object 2 destroyed! Object 1 destroyed!

Related questions

When was Object to Be Destroyed created?

Object to Be Destroyed was created in 1923.


What is full form of oop's?

Object Oriented Programming


Is PHP object oriented?

Yes - 'advanced' PHP programming uses Object Oriented Programming (OOP).


What is an essential of Object Oriented Programming?

The 3 essential concepts of Object Oriented Programming are:InheritanceEncapsulation &Polymorphism


What is object based programming language?

Object-based programming language is a language that supports all the features of object oriented programming features like classes,object,encapsulation ,abstraction,polymorphism etc except inheritence.


Full form of oops?

The full form of OOP is Object-Oriented Programming.


What actually mean by object oriented programming Is C plus plus is a object oriented programming?

Just eat a watermellon!


What is copy constructor in object oriented programming?

The purpose of constructor in object oriented programming is to initialize data. Likewise copy constructors are used to initialize an object with data from another object.


Difference between conventional programming and object oriented programming?

diff between oops and conventional programming


Can we create an array of objects for a class having default constructor Justify your answer?

See example code below. #include <iostream> class x { private: // Members. static int counter; int data; public: // Default constructor and destructor. x():data(++counter){printf("Object %d created!\n",data);} ~x(){printf("Object %d destroyed!\n",data);} // other members omitted for brevity... }; // Initialise static counter. int x::counter=0; int main() { // Instantiate an array of 10 objects. x arr[10]; // .. do some work with the array .. return( 0 ); // The array falls from scope, destroying the objects. } Example output: Object 1 created! Object 2 created! Object 3 created! Object 4 created! Object 5 created! Object 6 created! Object 7 created! Object 8 created! Object 9 created! Object 10 created! Object 10 destroyed! Object 9 destroyed! Object 8 destroyed! Object 7 destroyed! Object 6 destroyed! Object 5 destroyed! Object 4 destroyed! Object 3 destroyed! Object 2 destroyed! Object 1 destroyed!


What are the limitation of structured programming?

By itself, structured programming does not support the notion of a function call. This is achieved through an extension of structured programming known as procedural programming. Object-oriented programming extends procedural programming such that data and the functions that operate upon the data can be encapsulated within an object.


Why did computer scientists introduce object - oriented programming?

If you work a while with object-oriented programming, you'll notice that it offers huge benefits over the traditional approach. In fact, you would rather not use a programming language that doesn't have at least the option of object-oriented programming, if you have the choice.