ShoGun
asked on
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!
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!
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 ?
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 ?
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.j ava: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-- ;
}
}
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.j
at Example1.main(Compiled Code)
class Example1
{
//to make once only,frm,bruce static initalisatior
static
{
System.runFinalization() ;
System.runFinalizersOnExit
}
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
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-- ;
}
}
ASKER
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
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
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 :)
your question is simply incorrect. if you really need this behaviour - choose some other language :)
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.
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.
Yaa. I don't fine need for Destructors in hava which has automatic garbage collection
ASKER
thanks for informatoin!
http://www.firstsql.com/java/gotchas/
search around 'destruct()'
http://idm.internet.com/opinion/java_v_cpp.shtml
search around 'super.finalize()'
see if useful
http://www.cs.brown.edu/courses/cs032/resources/C++tutorial.html