Results 1 to 7 of 7
  1. #1
    fahadmunir32 is offline Banned
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default What is The Working OF A Funtion void finalize( ){} in Java

    what is the function of garbage collector in java.. and void finalize()P{} Funtion .. How They Work.. What.. One Can Benifit.. in A Programm..
    Plz Give Any Example...

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,379
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is The Working OF A Funtion void finalize( ){} in Java

    Moved to the New To Java forum; the answer to this question can be found in a tutorial for the basics of the Java language.

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    heatblazer is offline Senior Member
    Join Date
    Nov 2012
    Posts
    136
    Rep Power
    0

    Default Re: What is The Working OF A Funtion void finalize( ){} in Java

    As far as I know, you`d better avoid using it.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,379
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is The Working OF A Funtion void finalize( ){} in Java

    Quote Originally Posted by heatblazer View Post
    As far as I know, you`d better avoid using it.
    True; puzzle: will objects of this class ever be collected?

    Java Code:
    public class Zombie {
       private static List<Zombie> zombies= new ArrayList<Zombie>();
       ...
       public void finalize() {
          zombies.add(this);
       }
    }
    (hint: the answer is in the JLS)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    b4viral is offline Member
    Join Date
    Jul 2012
    Posts
    17
    Rep Power
    0

    Default Re: What is The Working OF A Funtion void finalize( ){} in Java


  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,418
    Rep Power
    5

    Default Re: What is The Working OF A Funtion void finalize( ){} in Java

    Ok, I'll bite. The static will hang around for ever since an instance isn't required to access it. Except that it is private and can't be accessed. So it is pretty useless except from another Zombie class.

    According to the JLS,
    "The Java programming language does not specify how soon a finalizer will be invoked, except to say that it will happen before the storage for the object is reused."

    So you save the reference in an unaccessible List and then the gc runs but can't reclaim it since it is still "accessible". But if I add a public static method to the Zombie class to return the list then I could get access to it. So to be safe, it should never be reclaimed because it could cause all sorts of security nightmares. Imagine accessing what one thought was an reference to a zombie class but found out that it was reclaimed and is now something else.

    Was I close?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,379
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is The Working OF A Funtion void finalize( ){} in Java

    Quote Originally Posted by jim829 View Post
    Ok, I'll bite. The static will hang around for ever since an instance isn't required to access it. Except that it is private and can't be accessed. So it is pretty useless except from another Zombie class.

    According to the JLS,
    "The Java programming language does not specify how soon a finalizer will be invoked, except to say that it will happen before the storage for the object is reused."

    So you save the reference in an unaccessible List and then the gc runs but can't reclaim it since it is still "accessible". But if I add a public static method to the Zombie class to return the list then I could get access to it. So to be safe, it should never be reclaimed because it could cause all sorts of security nightmares. Imagine accessing what one thought was an reference to a zombie class but found out that it was reclaimed and is now something else.

    Was I close?

    Regards,
    Jim
    Yep, but no matter whether that list is public or private, no Zombie elements will be reclaimed by the GC; if I'd be able to clear the list, the objects will be reclaimed because the finalizer will not be run again.Because of this 'zombie resurrection', this rule was added to early versions of Java.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Please explain static void and void methods are ?
    By shavindra in forum New To Java
    Replies: 5
    Last Post: 01-10-2013, 08:46 PM
  2. Replies: 1
    Last Post: 03-25-2012, 02:40 AM
  3. how to resolve last funtion
    By spinter in forum Advanced Java
    Replies: 2
    Last Post: 10-28-2011, 01:35 AM
  4. Replies: 5
    Last Post: 01-24-2008, 11:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •