equivalent of destrcutor in Java?

what i want is to count creation of objects and after 10 creatyed then tell me to delete one prevous,so any tinme only 10 there.

is finalize() sitable?
in constructor i increade count and in destructor i decrease count,os in a static int counter I get all my number objet of class mine?

i read "Thinking in java" and Brice say
'Garbage collection is not destruction'

help!
ShoGunAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbormannCommented:
0
heyhey_Commented:
you don't have destructors in Java (because of automatic garbage collection), but you can always implement your own
init() and destroy() methods, and call them whenever you need to initialize / destroy something (nice technique is to use finaly statement

try
{
  obj.init();
  obj.doIt();
  ...
  obj.doIt2();
} finally
{
  obj.destruct();
}

but I really don't understand what is your question ?
0
g_senthilkumarCommented:
hi,
  I couldnt get your question.But finalize() method will be called before the garbage collection.That is whenever garbage collection occurs,in the first phase it will call finalize() method and in the second phase it will perform garbage collection.
   But when garbage collection will occur we cant say sure.Even if you call System.gc(),the JVM will take maxmimum effort to perform garbage collection.But that is not sure.
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

mbormannCommented:
ShoGun,
is this what u r asking for?

>>>>>count creation of objects and after 10 creatyed
then tell me to delete one prevous,so any tinme only 10 there.

you mean that any time there will be only 10 objects of ur class in Memory,(tracked thru counter)and you want to delete / free up the eleventh object?

If that is so i am afraid that currently Java does not provide this functionality as u can never manually delete the object ,thats the job of the JVM.
from the first URL link..... and heyhey's comment

Destructors
  In most circumstances, finalize() is useless as an object destructor. If a class has a
  finalize() method, Java will only call it during garbage collection. However, Java does
  not guarantee when or in what order it will call finalize() methods. An object may
  never be garbage collected, so finalize() is not called even when the application
  terminates. Additionally, there is no finalize() for classes—finalize() can't be static.

  The unpredictable nature of finalize() is not acceptable, except in rare cases.
  Classes that require cleanup after all access is complete should use explicit
  destructor methods. Possible actions by destructor methods include the following:

   Flushing file buffers.
   Closing external file access.
   Disconnecting from referencing objects and classes-vectors, hashtables and other
  links.
   Removing the object from screen display.
   Sending destructors to child or linked objects.

  You define object destructors as instance methods and class destructors as static.
  Like constructors, user-defined destructors for a derived class normally call the
  destructor for the base class:

  public void destruct()
  {
     ...
     super.destruct();
  }

  The Java compilers don't recognize user-defined destructors as anything special, so
  applications code must use them properly. You need to perform the following steps:

   1.Ensure the destructor is always called.
   2.Deal with multiple references to the object or class, perhaps using a reference
      count.
   3.Avoid further use of the object or class after calling the destructor method.

Summary: Instead of finalize(), create user-defined destructors for objects and classes, as needed, and ensure proper use by application code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ShoGunAuthor Commented:
here my outpt and then code,now people understand? tell so

D:\>java Example1
Create object=0
Create object=1
Create object=2
Create object=3
Create object=4
Create object=5
Create object=6
Create object=7
Create object=8
Create object=9
java.lang.Exception: The objects exceded max 10
        at Example1.<init>(Example1.java:18)
        at Example1.main(Compiled Code)



class Example1
{
      //to make once only,frm,bruce static initalisatior
      static
      {
            System.runFinalization() ;
            System.runFinalizersOnExit( true );
      }
      
      static int ID = 0 ;
      static int currr = 0 ;
      static int max = 10 ;
      
      public Example1() throws Exception
      {
            if(currr >= max)
            {
                  throw new Exception("The objects exceded max 10") ;
            }
            
            System.out.println("Create object=" + this.ID) ;
            ID++ ;
            currr++ ;
      }
      //thsi mean current object

      public static void main(String []args)throws Exception
      {
            for(int i = 0; i < 13; i++)//test with more thasn 10
            {
                  new Example1() ;
                  Thread.sleep(3000) ;//hope garbage strats
            }
      }
      
      protected void finalize() throws Throwable
      {
            //from java_v_cpp.shtml
            super.finalize() ;
            System.out.println("Clean object=" +  this.ID) ;
            currr-- ;
      }
}
0
ShoGunAuthor Commented:
what i wnat is

Create object=0
Create object=1
Create object=2
Create object=3
Create object=4
Create object=5
Clean object=5 --
Create object=6
Create object=7
Clean object=6 --
Create object=8
Create object=9


0
heyhey_Commented:
Java is Java. Objects live their own life. if you want to clean something you have to call some cleanup method directly. if you don't use some Object you simply FORGET about it - GC will free the memory when needed.

your question is simply incorrect. if you really need this behaviour - choose some other language :)
0
vivexpCommented:
Hi,
Hope this info is useful to u....
Finalizer methods are almost the opposite of constructor methods; whereas a constructor method is used to initialize an object, finalizer methods are called just
before the object is garbage-collected and its memory reclaimed.

The finalizer method is named simply finalize(). The Object class defines a default finalizer method, which does nothing. To create a finalizer method for your
own classes, override the finalize() method using this signature:

     protected void finalize() throws Throwable {
         super.finalize();
     }



Inside the body of that finalize() method, include any cleaning up you want to do for that object. You can also call super.finalize() to allow your class's
superclasses to finalize your object, if necessary (it's a good idea to do so just to make sure that everyone gets a chance to deal with the object if they need to).

You can always call the finalize() method yourself at any time; it's just a plain method like any other. However, calling finalize() does not trigger an object to
be garbage-collected. Only removing all references to an object will cause it to be marked for deleting.

Finalizer methods are best used for optimizing the removal of an object-for example, by removing references to other objects, by releasing external resources that
have been acquired (for example, external files), or for other behaviors that may make it easier for that object to be removed.
0
Ravindra76Commented:

 Yaa. I don't fine need for Destructors in hava which has automatic garbage collection
0
ShoGunAuthor Commented:
thanks for informatoin!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.