java has automatic garbage collector.we can define finalize()method that will be run by the garbage collector.if we explicitely define system.gc()in a program then can the same program have finalize() method?if yes, then how will the code written in the finalize()be executed?by whom?and how to transfer the program flow to the finalize()method? if the program can contain the finalize()method and also System.gc()then what happens if System.gc()gets executed beforew the finalize()method? thank you.
The method System.gc() tells the JVM that running the garbage collector now might be a good idea. It's a suggestion, not a command, and the JVM can ignore it. Before an object is garbage collected, the collector will call finalize() on that one object, yes. There's no guarantee, though, that any given object will ever be collected, you realize. You can't redefine the System.gc() method, so most of your question doesn't make any sense.
To take Ernest's discussion a little further, because the GC runs unpredictably using the finalize() method is not generally recommended. It is there, and IF the GC runs, it will call an object's finalize() method before destroying the object, but it's too erratic to rely on for anything important.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
thank you very much for clearing my concept!i am preparing for java programmer certificcation and a lot of such small things that require basics haunt my mind . I am sorry to ask such a basic question. thanks again