-3

i want to call destructor, when i close object, but i dont know how or when,.. I try whit finalize but its not calling. I dont know why

There is example code, i try with debugger but its help me..

class tralala(){ 

    int i=0;

    tralala(){
    i=i+1;}
    protected void finalize( ){
      i=i-1;
    }}

But it isnt call i dont know why. If anyone have got a suggestion plis help me. I am in the stuck at the moment, but i am not shure if java has got destructors.

user
  • 86,916
  • 18
  • 197
  • 190
Papi
  • 97
  • 2
  • 9
  • 3
    Please, use nicer code formatting. At first I thought this wouldn't even compile. This is ugly any unmaintainable. Also, keep in mind, that according to the naming convention for Java, you should have classes names starting with Capital Letters... – ppeterka Sep 30 '13 at 09:51
  • There are no destructors in Java. Your question is therefore about nothing. – user207421 Sep 30 '13 at 10:27
  • possible duplicate of [Is there a destructor for Java?](http://stackoverflow.com/questions/171952/is-there-a-destructor-for-java) – Raedwald Apr 23 '15 at 05:52

5 Answers5

2

I think you are trying to implement similar behavior like destructors. As destructors are not available into Java, I think you are trying to do with finalize.

But the finalize method is called when an object is about to get garbage collected. That can be at any time after it has become eligible for garbage collection. Note that it's entirely possible that an object never gets garbage collected (and thus finalize is never called). This can happen when the object never becomes eligible for gc (because it's reachable through the entire lifetime of the JVM) or when no garbage collection actually runs between the time the object become eligible and the time the JVM stops running (this often occurs with your programs).


Read more about When is the finalize() method called in Java? and Why not to use finalize() method in java


And read How to implement object counter in Java SO QA for your solution. This is used for object counter. You can use same logic.

Community
  • 1
  • 1
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
2

Method finalize( ) "сalled by the garbage collector on an object when garbage collection determines that there are no more references to the object". So it will be called after garbage collector observe it to delete, but not immediately when object leave the scope.

I rly can't understand what do you mean as "close object"? You should find some other architecture solution to do what you want: maybe just call your method or raise some custom event with supplied handler when object leave scope ("close object").

Pavlo K.
  • 371
  • 1
  • 9
1

There is no destructors in java

class Tralala(){ 

    int i=0;

    Tralala(){
    i=i+1;}

    protected void reset( ){
      i=i-1;
    }}

call reset method when ever you want to destruct.

As a side note:There are lots of corrections in your code like naming conventions, formatting and also providing modifiers.

And I'm not getting, when finalize called, your instance is no more. Why you are resetting at that time,Since after that NO MORE OPERATIONS ON THAT OBJECT.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

JAVA does not have destructors. JAVA has Garbage collectors. Garbage Collector runs as a thread and automatically destroys instances which do not have any active references.

Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
1

it depends on your definition of closing the class.

you could implement the interface Closable so that eclipse could warn you about not closing the instance when needed.

it's different from finalize since you need to call it by yourself.

you could call close() in the finalize function in order to make sure it will get called when the instance is GC'ed .

android developer
  • 114,585
  • 152
  • 739
  • 1,270