java class destroy's another class












-1















I was coding my AI project in java while I ran into this problem. I first generate the environment of some game using class "Pasture". Then the intelligent agent which is written in class "Shepherd" ask's some questions from the "Pasture" class using it's public interface and then moves based on the results. Now, when certain conditions hold, the game ends and I can simply print the score and then call System.exit(0) to end the program. But, I want to start a new game when the game ends, instead of just ending the whole program. Now I need to destroy the "Shepherd" class to end the agent and then start a new game. I know that java classes has destructors. The question is:
can I call the destructor of another java class(in this case the class "Shepherd") inside another class(in this case the class "Pasture")?
and How can I write a destructor method? What is the syntax?



Thanks, everyone. I got it. I was wrong about destructors. I just thought that like c++, java also has destructors.










share|improve this question




















  • 2





    You cannot forcefully "destroy" an object. The garbage collector will eventually do that when the object is no longer reachable.

    – Henry
    Jan 19 at 6:39













  • "I know that java classes has destructors" – Wait, what?

    – MC Emperor
    Jan 19 at 8:40
















-1















I was coding my AI project in java while I ran into this problem. I first generate the environment of some game using class "Pasture". Then the intelligent agent which is written in class "Shepherd" ask's some questions from the "Pasture" class using it's public interface and then moves based on the results. Now, when certain conditions hold, the game ends and I can simply print the score and then call System.exit(0) to end the program. But, I want to start a new game when the game ends, instead of just ending the whole program. Now I need to destroy the "Shepherd" class to end the agent and then start a new game. I know that java classes has destructors. The question is:
can I call the destructor of another java class(in this case the class "Shepherd") inside another class(in this case the class "Pasture")?
and How can I write a destructor method? What is the syntax?



Thanks, everyone. I got it. I was wrong about destructors. I just thought that like c++, java also has destructors.










share|improve this question




















  • 2





    You cannot forcefully "destroy" an object. The garbage collector will eventually do that when the object is no longer reachable.

    – Henry
    Jan 19 at 6:39













  • "I know that java classes has destructors" – Wait, what?

    – MC Emperor
    Jan 19 at 8:40














-1












-1








-1








I was coding my AI project in java while I ran into this problem. I first generate the environment of some game using class "Pasture". Then the intelligent agent which is written in class "Shepherd" ask's some questions from the "Pasture" class using it's public interface and then moves based on the results. Now, when certain conditions hold, the game ends and I can simply print the score and then call System.exit(0) to end the program. But, I want to start a new game when the game ends, instead of just ending the whole program. Now I need to destroy the "Shepherd" class to end the agent and then start a new game. I know that java classes has destructors. The question is:
can I call the destructor of another java class(in this case the class "Shepherd") inside another class(in this case the class "Pasture")?
and How can I write a destructor method? What is the syntax?



Thanks, everyone. I got it. I was wrong about destructors. I just thought that like c++, java also has destructors.










share|improve this question
















I was coding my AI project in java while I ran into this problem. I first generate the environment of some game using class "Pasture". Then the intelligent agent which is written in class "Shepherd" ask's some questions from the "Pasture" class using it's public interface and then moves based on the results. Now, when certain conditions hold, the game ends and I can simply print the score and then call System.exit(0) to end the program. But, I want to start a new game when the game ends, instead of just ending the whole program. Now I need to destroy the "Shepherd" class to end the agent and then start a new game. I know that java classes has destructors. The question is:
can I call the destructor of another java class(in this case the class "Shepherd") inside another class(in this case the class "Pasture")?
and How can I write a destructor method? What is the syntax?



Thanks, everyone. I got it. I was wrong about destructors. I just thought that like c++, java also has destructors.







java destructor






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 19 at 9:34







Amir Valizadeh

















asked Jan 19 at 6:34









Amir ValizadehAmir Valizadeh

44




44








  • 2





    You cannot forcefully "destroy" an object. The garbage collector will eventually do that when the object is no longer reachable.

    – Henry
    Jan 19 at 6:39













  • "I know that java classes has destructors" – Wait, what?

    – MC Emperor
    Jan 19 at 8:40














  • 2





    You cannot forcefully "destroy" an object. The garbage collector will eventually do that when the object is no longer reachable.

    – Henry
    Jan 19 at 6:39













  • "I know that java classes has destructors" – Wait, what?

    – MC Emperor
    Jan 19 at 8:40








2




2





You cannot forcefully "destroy" an object. The garbage collector will eventually do that when the object is no longer reachable.

– Henry
Jan 19 at 6:39







You cannot forcefully "destroy" an object. The garbage collector will eventually do that when the object is no longer reachable.

– Henry
Jan 19 at 6:39















"I know that java classes has destructors" – Wait, what?

– MC Emperor
Jan 19 at 8:40





"I know that java classes has destructors" – Wait, what?

– MC Emperor
Jan 19 at 8:40












2 Answers
2






active

oldest

votes


















0















Now I need to destroy the "Shepherd" class to end the agent and then start a new game. I know that java classes has destructors.




You have misunderstood something fairly fundamental:




  1. Java doesn't have destructors. You might be confusing destructors with finalizers. These are methods that are called after the GC has decided to delete an object. (This is an oversimplification ... but the real point is that finalizers are not relevant to this problem. In fact, there very few cases where finalizers are relevant.)


  2. You can't destroy objects. Objects are destroyed by the garbage collector when they are no longer needed. More specifically, they are destroyed when they are unreachable; i.e. when they can no longer influence the execution of the program.



So what do you do?



First of all, forget about "destroying" objects. Instead think about how to prepare for the next game. There are two approaches.




  1. You could implement a reset() or similar method on all "game" objects that need to be reset / reinitialized when your start a new game.


  2. You could simply drop all of the relevant "game" object on the floor and create new ones. (The GC will take care of the garbage.)



Or you could use a combination of the two approaches; e.g. reset the Pasture object to its initial state and discard / recreate the Shepard, Sheep and so on.






share|improve this answer

































    0














    There are no destructors in java that you can program or call. To tell the garbage collector that you are not using an object anymore you can set it to null



    myObject = null;


    If you have some resources that needs to be closed/freed you can of course create your own destruct method but you have to call it manually



    myObject.closeResources();
    myObject = null;


    Or if you want to reuse an object you could have a reset method that sets the object in the same state as it was when it was first created you could make your constructor call the same method and then you do it manually.



    public class MyClass {
    public MyClass() {
    this.reset();
    }
    }

    MyClass myObject = new MyClass();
    ...

    myObject.reset();





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54264673%2fjava-class-destroys-another-class%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0















      Now I need to destroy the "Shepherd" class to end the agent and then start a new game. I know that java classes has destructors.




      You have misunderstood something fairly fundamental:




      1. Java doesn't have destructors. You might be confusing destructors with finalizers. These are methods that are called after the GC has decided to delete an object. (This is an oversimplification ... but the real point is that finalizers are not relevant to this problem. In fact, there very few cases where finalizers are relevant.)


      2. You can't destroy objects. Objects are destroyed by the garbage collector when they are no longer needed. More specifically, they are destroyed when they are unreachable; i.e. when they can no longer influence the execution of the program.



      So what do you do?



      First of all, forget about "destroying" objects. Instead think about how to prepare for the next game. There are two approaches.




      1. You could implement a reset() or similar method on all "game" objects that need to be reset / reinitialized when your start a new game.


      2. You could simply drop all of the relevant "game" object on the floor and create new ones. (The GC will take care of the garbage.)



      Or you could use a combination of the two approaches; e.g. reset the Pasture object to its initial state and discard / recreate the Shepard, Sheep and so on.






      share|improve this answer






























        0















        Now I need to destroy the "Shepherd" class to end the agent and then start a new game. I know that java classes has destructors.




        You have misunderstood something fairly fundamental:




        1. Java doesn't have destructors. You might be confusing destructors with finalizers. These are methods that are called after the GC has decided to delete an object. (This is an oversimplification ... but the real point is that finalizers are not relevant to this problem. In fact, there very few cases where finalizers are relevant.)


        2. You can't destroy objects. Objects are destroyed by the garbage collector when they are no longer needed. More specifically, they are destroyed when they are unreachable; i.e. when they can no longer influence the execution of the program.



        So what do you do?



        First of all, forget about "destroying" objects. Instead think about how to prepare for the next game. There are two approaches.




        1. You could implement a reset() or similar method on all "game" objects that need to be reset / reinitialized when your start a new game.


        2. You could simply drop all of the relevant "game" object on the floor and create new ones. (The GC will take care of the garbage.)



        Or you could use a combination of the two approaches; e.g. reset the Pasture object to its initial state and discard / recreate the Shepard, Sheep and so on.






        share|improve this answer




























          0












          0








          0








          Now I need to destroy the "Shepherd" class to end the agent and then start a new game. I know that java classes has destructors.




          You have misunderstood something fairly fundamental:




          1. Java doesn't have destructors. You might be confusing destructors with finalizers. These are methods that are called after the GC has decided to delete an object. (This is an oversimplification ... but the real point is that finalizers are not relevant to this problem. In fact, there very few cases where finalizers are relevant.)


          2. You can't destroy objects. Objects are destroyed by the garbage collector when they are no longer needed. More specifically, they are destroyed when they are unreachable; i.e. when they can no longer influence the execution of the program.



          So what do you do?



          First of all, forget about "destroying" objects. Instead think about how to prepare for the next game. There are two approaches.




          1. You could implement a reset() or similar method on all "game" objects that need to be reset / reinitialized when your start a new game.


          2. You could simply drop all of the relevant "game" object on the floor and create new ones. (The GC will take care of the garbage.)



          Or you could use a combination of the two approaches; e.g. reset the Pasture object to its initial state and discard / recreate the Shepard, Sheep and so on.






          share|improve this answer
















          Now I need to destroy the "Shepherd" class to end the agent and then start a new game. I know that java classes has destructors.




          You have misunderstood something fairly fundamental:




          1. Java doesn't have destructors. You might be confusing destructors with finalizers. These are methods that are called after the GC has decided to delete an object. (This is an oversimplification ... but the real point is that finalizers are not relevant to this problem. In fact, there very few cases where finalizers are relevant.)


          2. You can't destroy objects. Objects are destroyed by the garbage collector when they are no longer needed. More specifically, they are destroyed when they are unreachable; i.e. when they can no longer influence the execution of the program.



          So what do you do?



          First of all, forget about "destroying" objects. Instead think about how to prepare for the next game. There are two approaches.




          1. You could implement a reset() or similar method on all "game" objects that need to be reset / reinitialized when your start a new game.


          2. You could simply drop all of the relevant "game" object on the floor and create new ones. (The GC will take care of the garbage.)



          Or you could use a combination of the two approaches; e.g. reset the Pasture object to its initial state and discard / recreate the Shepard, Sheep and so on.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 19 at 9:57

























          answered Jan 19 at 9:51









          Stephen CStephen C

          517k70568925




          517k70568925

























              0














              There are no destructors in java that you can program or call. To tell the garbage collector that you are not using an object anymore you can set it to null



              myObject = null;


              If you have some resources that needs to be closed/freed you can of course create your own destruct method but you have to call it manually



              myObject.closeResources();
              myObject = null;


              Or if you want to reuse an object you could have a reset method that sets the object in the same state as it was when it was first created you could make your constructor call the same method and then you do it manually.



              public class MyClass {
              public MyClass() {
              this.reset();
              }
              }

              MyClass myObject = new MyClass();
              ...

              myObject.reset();





              share|improve this answer




























                0














                There are no destructors in java that you can program or call. To tell the garbage collector that you are not using an object anymore you can set it to null



                myObject = null;


                If you have some resources that needs to be closed/freed you can of course create your own destruct method but you have to call it manually



                myObject.closeResources();
                myObject = null;


                Or if you want to reuse an object you could have a reset method that sets the object in the same state as it was when it was first created you could make your constructor call the same method and then you do it manually.



                public class MyClass {
                public MyClass() {
                this.reset();
                }
                }

                MyClass myObject = new MyClass();
                ...

                myObject.reset();





                share|improve this answer


























                  0












                  0








                  0







                  There are no destructors in java that you can program or call. To tell the garbage collector that you are not using an object anymore you can set it to null



                  myObject = null;


                  If you have some resources that needs to be closed/freed you can of course create your own destruct method but you have to call it manually



                  myObject.closeResources();
                  myObject = null;


                  Or if you want to reuse an object you could have a reset method that sets the object in the same state as it was when it was first created you could make your constructor call the same method and then you do it manually.



                  public class MyClass {
                  public MyClass() {
                  this.reset();
                  }
                  }

                  MyClass myObject = new MyClass();
                  ...

                  myObject.reset();





                  share|improve this answer













                  There are no destructors in java that you can program or call. To tell the garbage collector that you are not using an object anymore you can set it to null



                  myObject = null;


                  If you have some resources that needs to be closed/freed you can of course create your own destruct method but you have to call it manually



                  myObject.closeResources();
                  myObject = null;


                  Or if you want to reuse an object you could have a reset method that sets the object in the same state as it was when it was first created you could make your constructor call the same method and then you do it manually.



                  public class MyClass {
                  public MyClass() {
                  this.reset();
                  }
                  }

                  MyClass myObject = new MyClass();
                  ...

                  myObject.reset();






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 19 at 9:27









                  Joakim DanielsonJoakim Danielson

                  8,1823724




                  8,1823724






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54264673%2fjava-class-destroys-another-class%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Liquibase includeAll doesn't find base path

                      How to use setInterval in EJS file?

                      Petrus Granier-Deferre