Sunday 15 February 2015

java - Method to execute when a class instance falls out of scope? -


I have a C ++ class that is used to enter and exit the routine used.

  class myTrace {std :: string m_strName; MyTrace (std :: string strName) {m_strName = strName; _tprintf (_T ("enter \ n% s"), m_strName); }; ~ MyTrace () {_tprintf (_T ("\ n% s nickel"), m_strName); }; };   

It is easy to use, you do it for the first time, using statm mechanism. When the routine is called it print out and the entry message when the regular exit, it prints an eclipse message.

I am trying to migrate it to Java, but I want to read about the absence of all the methods I am confused about that in any way it should be done in Java Could?

- Bob Lambert -

Just in theory, you finalize the method , Therefore

  public class matches {@Override protected void finalize ()) Throwable {System.out.println ("nickel") throws; Super.finalize ();  

This garbage collector will be called when it determines that there is no more reference to the object

so when you do

  the public is stable Zero myMethod () {MyClass a = new MyClass (); } Public static zero principal (string [] args) {myMethod (); System.gc (); }   

As output you will

get rid

if I remember in C ++ Destructors The object will be destroyed for the release memory, in java we do not have to worry about it, ji is doing this for us, in most cases you do not need a destructus

No comments:

Post a Comment