Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30485

finalize() not called.

Newsgroups comp.lang.java.programmer
Date 2014-12-04 08:56 -0800
Message-ID <850b3ab9-57ea-490c-a165-943183169841@googlegroups.com> (permalink)
Subject finalize() not called.
From somenathpal@gmail.com

Show all headers | View raw


I have the following class
public class Book {
	boolean checkout = false;

	public Book(boolean checkout) {
		
		this.checkout = checkout;
	}
	void checkIn() {
		
		checkout = false;
	}
	
	protected  void  finalize() {
		if (checkout) {
			System.out.println("Error Checked Out !!");
		}
		
	}
}

==========================================================================
And it is used as follows

public class Test {

	public static void main(String[] args) {
		Book novel  =new Book(true);
		novel.checkIn();
		Book book = new Book(true);
		//System.out.println("Calling GC");
		System.gc();

	}

}
Now when System.gc() is called I expect finalize() to be called. But it does not get called. But when I uncomment the following
 System.out.println("Calling GC"); the finalize() gets called. I am not able to understand this behavior. I was expecting finalize() method yo be called. 
Could you please explain the behavior. 

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

finalize() not called. somenathpal@gmail.com - 2014-12-04 08:56 -0800
  Re: finalize() not called. Robert Klemme <shortcutter@googlemail.com> - 2014-12-04 20:37 +0100
  Re: finalize() not called. v_borchert@despammed.com (Volker Borchert) - 2014-12-04 19:05 +0000
  Re: finalize() not called. Joshua Cranmer 🐧 <Pidgeot18@verizon.invalid> - 2014-12-04 14:24 -0600
  Re: finalize() not called. Eric Sosman <esosman@comcast-dot-net.invalid> - 2014-12-06 16:07 -0500
    Re: finalize() not called. Lew <lewbloch@gmail.com> - 2015-01-04 16:13 -0800

csiph-web