How To Debug Override finalize() Method In Java?

476 views Asked by At

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...???

0

There are 0 answers