aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Examsimulator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from Examsimulator" Watch "Question from Examsimulator" New topic
Author

Question from Examsimulator

GopiKrishnan Chelliah
Greenhorn

Joined: Jan 27, 2008
Posts: 10
It prints infinite loop. i don't know why it calls finalize method? please explain.
class Box{
private int iVolume;
Box(int iVolume){
this.iVolume=iVolume;
}
public void finalize(){
System.out.println("finalizing a Box");
}
}

public class EdGrundy{
boolean stop = false;
public static void main(String argv[]){
new EdGrundy();
}
EdGrundy(){
while(stop==false){
new Box(99);
}
}
}
ishmayel vemuru
Ranch Hand

Joined: Jun 13, 2007
Posts: 46
Hi GopiKrishnan Chelliah..
Thi objects of the box Class are not using any where.
If any of the ( BOX ) object is not in use JVM will distroy that( BOX ) objet. Before destroy that type of objects( Un used object) the JVM will cal the finalize method in the corresponding object class if available (finalize method).
Thats why you are getting the out put like..
finalizing a Box
finalizing a Box
finalizing a Box
finalizing a Box
finalizing a Box
finalizing a Box
finalizing a Box
finalizing a Box
finalizing a Box
....
.
.
etc.
I hope you under stand this.
Havae a nice day.
---
Ishmayel
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
Do you understand the circumstances under which finalize gets called (This question comes from my web site, any other Archers fans out there?)


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
ishmayel vemuru
Ranch Hand

Joined: Jun 13, 2007
Posts: 46
Hi Marcus Green....
I didn't get your point..
Do you understand the circumstances under which finalize gets called
Havae a nice day..
----
Ishmayel.
Thirugnanam Saravanan
Ranch Hand

Joined: Dec 13, 2007
Posts: 81
OK Let me confirm my understanding with finalize() method.

1. finalize() method will be called by Garbage collector before Garbage Collecting.

2. Normally it should be overridden to clean-up non-Java resources used in a Java Code.

3. finalize() is never run more than once on any object.

4. Since we can't rely on Garbage Collection in Java, we can't assure that finalize() method will always be invoked. Hence we should not have any important business logic inside finalize() method.


Saravanan
SCJP 5.0(98%), SCWCD 5.0 (100%), OCA
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from Examsimulator