0

As we know about this like...

The java.lang.Object.finalize() is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup. Also The finailze() method should be overridden for an object to include the clean up code or to dispose of the system resources that should to be done before the object is garbage collected.

So I created a class and override finalize() method in it with some of mine code as shown below as my main concept is to write all the data that my class contain in the end like before closing the form in a text file to save it...

public class MainClass
{
    public static void main(String[] args) {
        NewDEMOClass obj = new NewDEMOClass();
        obj.AddValueToMap("A",1);
        obj.AddValueToMap("B",2);
        obj.AddValueToMap("C",3);
        // Many Value Added Here...         
    }    
}

Then my custom class is...

public class NewDEMOClass
{
    private Map<String, Integer> customDictionary = new HashMap<String, Integer>();

    public NewDEMOClass()
    {   

    }

    public AddValueToMap(String Key, Integer Value)
    {   
        customDictionary.put(Key, Value);
    }           

    @Override
    protected void finalize()
    {
        String totalWord = "";
        for ( Map.Entry<String, Integer> entry : customDictionary.entrySet() ) {
            totalWord += entry.getKey() + "-" + entry.getValue() + System.lineSeparator();
        }
        while (true)
        {
            try(
                PrintWriter out = new PrintWriter("customDictionary.txt", "UTF-8")  ){
                out.println( totalWord );
                break;
            } catch (FileNotFoundException ex) {
                Logger.getLogger(AutoUrduPredictorAndSuggester.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(AutoUrduPredictorAndSuggester.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}

So my main problem and question is that I want to debug finalize() method inner codes line by line to see the errors and etc But I am not able to add breakpoint as I am using NetBeans IDE. So What to do now and How...???

Jack
  • 131,802
  • 30
  • 241
  • 343
  • 1
    Why would you need a debugger to see the errors? That's just what the logger is there for amongst others... And a `finalize`-method with an endless-loop is most definitely one of the worst ideas I could think of. As for the issue with netbeans: just use another debugger (works in IntelliJ, I think JDB supports this as well). And last but not least using `finalize` is just in general bad. Use a regular method instead. –  Dec 31 '16 at 13:57
  • 2
    You should just never override finalize(). It's as simple as that. If you need to do that, use a regular method, and call it when necessary. – JB Nizet Dec 31 '16 at 13:58
  • 3
    You don't have many guarantees about `finalize()`: you can't know _when_, you can't know _who_ (which thread) and you can't even be sure it will be called _at all_. – Jack Dec 31 '16 at 13:59
  • I just want to write my data when the Apps close completely. So will the `finalize()` method run or still not? Any suggestion under my idea beside `finalize()` method to achieve what I want? – Sahar Jabeen Dec 31 '16 at 14:06
  • 1
    No, you don't have any guarantee that a finalize() method will ever be called. As I said, if you need to do something when your app closes, then call a method explicitly when your app closes. – JB Nizet Dec 31 '16 at 14:11
  • @JBNizet Thanks for your answer and suggestion. Please add it in an answer in detail with limitations and cautions so I can select it. – Sahar Jabeen Dec 31 '16 at 14:13
  • That `finalize` method doesn't do what it is supposed to do: release resources. Instead, it allocates a whole bunch of new stuff and does all kinds of I/O and other crazy things. Finalizers are supposed to dispose of resources during times when memory is tight, not waste them. – Lew Bloch Dec 31 '16 at 22:34
  • 1
    Try to use `ShutDown Thread` instead of `finalize()` like below code in your Class Constructor... ` Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){public void run() {functionThatShouldWorkInFinalize();}}, "Shutdown-thread"));` – Muhammad Hassan Jan 02 '17 at 13:11

0 Answers0