article

The Story Of Resurrection

Email
Submitted on: 1/3/2015 6:56:00 PM
By: Abdul Rafay Mansoor (from psc cd)  
Level: Beginner
User Rating: By 7 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 1654
 
     Explains the concepts of Garbage Collection and object resurrection in java. I have taken a novel approach so do comment/vote, please.

 
				

The Story Of Resurrection.



Once there lived an object of Criminal class, reffered as Rambo. he had many alias BigCat, BlackTiger, TheRock ...but they were all the same person.

He committed a serious crime for which he was to be removed from this world (the heap), all his Refrences and Alises where nulled from records (stack) so that ShootingSquard (System.gc()) could take him. Rambo knew that any Exception he may throw at the Squard will not save him since his hour has come. what did your ancestors do in their final hours ?? asked the squad, I dont know I have to recall it explicitly answered rambo.

As the traditions of the land (JVM), he was asked for any final wish before is death, the Squard promised to fulfill if in mood (?) , the cunning Rambo immediately asked for resurrection, bound by the promise the moody Squad let him go with a new Alias.

A few days later he was caught again for a similar crime, and was again awarded death , but this time no one asked his wish.

the code ! ! !


public class MainClass{
public static void main(String arg[]){

System.out.println("In the beginning there was .... \n");
Criminal Rambo=new Criminal();
Rambo.showIDcard("Rambo");
Criminal BigCat= Rambo; //Alias
BigCat.showIDcard("BigCat");
Rambo.doCrime();
System.out.println("Waiting of justice.... ");
Rambo=null;
BigCat=null;
System.gc();
Criminal.TheRock.showIDcard("TheRock");
Criminal.TheRock.doCrime();
System.out.println("Waiting of justice.... ");
Criminal.TheRock=null;
System.gc();
System.out.println("Criminal put to death without final wish ! ");
}
}


class Criminal {

public static Criminal TheRock;

public Criminal(){System.out.println("Criminal born ! ");}

public void showIDcard(String Alias){
System.out.println(Alias +" ID: "+this.hashCode());
}
public void doCrime(){
System.out.println("Criminal on rampage, Killed an innocent Object ");
}

public void finalize(){
System.out.println("Verdict: Death penalty ");
System.out.println("Criminal's last wish : resurrection !!! " );
TheRock=this;
System.out.println("Criminal given a new refrence: TheRock ");
throw new RuntimeException();
}
}


Out Put........

In the beginning there was ....
Criminal born !
Rambo ID: 15347198
BigCat ID: 15347198
Criminal on rampage, Killed an innocent Object
Waiting of justice....
Verdict: Death penalty
Criminal's last wish : resurrection !!!
Criminal given a new refrence: TheRock
TheRock ID: 15347198
Criminal on rampage, Killed an innocent Object
Waiting of justice....
Criminal put to death without final wish !




Moral of the Story.
  • Garbage Collector (gc) clears unrefrenced objects from the heap.
  • Every Object may define finilize(), executed just before the object is removed from the heap.
  • gc is moody, it works on its own will, System.gc() method is just a request not a compulsion (1.4).
  • It is not guaranteed that finilize() will be executed for an object, mostly it does, if it happens in the middle of a long execution.
  • any uncaught Exception thrown in finilize() is ignored.
  • if during finilize() of an unrefrenced object, it gets reffered to again (as above), then gc cannot not remove the object from heap.
  • if the object escapes the finilize() once, then the second time the finilize() is not called, the object is remove directly.
  • finilize() methods are not chained (they dont work like constructors), ie. sub class finilize() does not implicitly call super class finilize(), we have to explicitly call super.finilize();




your comment / votes are the source of encouragement, so please. - Rafay.


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.