Results 1 to 3 of 3

Thread: Need more information on finalize method.

  1. #1
    Junior Member
    Join Date
    Nov 2006
    Answers
    25

    Need more information on finalize method.

    This is the definition i got:

    Before an object is garbage collected, the runtime system calls its finalize() method. Your class can provide for its finalization simply by defining and implementing a method in your class named finalize().


    public class garbagecollector {

    public static void main(String args[])
    {
    System.out.println("a");
    System.out.println("b");
    }
    protected void finalize() throws Throwable {
    System.out.println("GC");
    }

    }

    ideally this should get the output
    abGC

    but i am getting only
    ab


    Why is this so?. and why the finalize() is not getting called?.
    Any help regarding this?. I am really confused about it.


  2. #2
    Junior Member
    Join Date
    Apr 2006
    Answers
    7

    Re: Need more information on finalize method.

    Hello TigerElango,

    We can only suggest the Garbagecollector ,but we can't force it ,rest it will does with its default behaviour.....
    we can suggest it like the code given below....

    public class Garbagecollector {

    public static void main(String args[])
    {
    System.out.println("a");
    System.out.println("b");

    Garbagecollector garbagecollector = new Garbagecollector();
    try {
    garbagecollector.finalize();
    } catch (Throwable e) {
    e.printStackTrace();
    }
    }

    protected void finalize() throws Throwable {
    System.out.println("GC");
    }
    }


  3. #3
    Junior Member
    Join Date
    Nov 2006
    Answers
    25

    Lightbulb Re: Need more information on finalize method.

    trycatch,

    Is there any Garbagecollector class in java. In jdk1.5.0 api there is no such class defined.

    Can u explain bit ellaborate on this.


    Regards,
    Elangovan R.


  4.  Sponsored Ads
    Ad


     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact