Is there a destructor for Java?

前端 未结 22 1578
[愿得一人]
[愿得一人] 2020-11-22 11:47

Is there a destructor for Java? I don\'t seem to be able to find any documentation on this. If there isn\'t, how can I achieve the same effect?

To make my question m

22条回答
  •  無奈伤痛
    2020-11-22 12:24

    I used to mainly deal with C++ and that is what lead me to the search of a destructor as well. I am using JAVA a lot now. What I did, and it may not be the best case for everyone, but I implemented my own destructor by reseting all the values to either 0 or there default through a function.

    Example:

    public myDestructor() {
    
    variableA = 0; //INT
    variableB = 0.0; //DOUBLE & FLOAT
    variableC = "NO NAME ENTERED"; //TEXT & STRING
    variableD = false; //BOOL
    
    }
    

    Ideally this won't work for all situations, but where there are global variables it will work as long as you don't have a ton of them.

    I know I am not the best Java programmer, but it seems to be working for me.

提交回复
热议问题