Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:53:36+00:00 2026-06-09T14:53:36+00:00

If I have a class delete itself, should its internal methods stop executing? I

  • 0

If I have a class delete itself, should its internal methods stop executing? I have a class B that tells class A to remove B from A’s ArrayList. I’m fairly certain B only exists in A’s ArrayList, and so when I remove it, it should be deleted, right? (NOTE: I’ve included a Serializable implementation just in case that would have anything to do with how the VM handles my classes, but I did not write in the read- and writeObject methods here. I doubt it will have anything to do with this issue though.)

public class A implements Serializable, B_Listener {
    ArrayList<B> bArray;

    public A() {
        bArray = new ArrayList<SomeObject>();
        bArray.add(new B(bArray.size(), this));
    }

    @Override
    public void deleteAtIndex(int index) {
        bArray.remove(index);
    }
}

public class B implements Serializable {
    B_Listener listener;
    int index;

    public B(B_Listener listener, int index) {
        this.listener = listener;
        this.index = index;
    }

    //This is called at some point in a B's lifetime.
    private void selfDestruct() {
        listener.deleteAtIndex(index);
        Log.w("B.class", "Should this not output? It does.");
    }
}

public interface B_Listener {
    public void deleteAtIndex(int index);
}

So the Log.w message executes when I don’t believe it should. Therefore, I’m afraid I’m creating java’s memory leaks. I’ve looked and looked throughout my code trying to find where B might be held by a pointer, but I’ve come up with nothing besides what I intended.

So then I’m asking if garbage is collected at a different time than when B is deleted on my end. If this is the case, then is it safe to say for the time being that I in fact am not holding objects I do not intend to hold?

Extra (NOTE: this might be hard to follow, everything above this should suffice for the problem):
I also have this tagged with Android-Views because I’m developing on Android: my B class holds View objects that point to B as a listener. When I removeView, the View manager or whatever Android has should no longer point to the View, I believe, and when I delete B, all its internal Views should also be deleted, meaning they can no longer hold B in existence by their own listener pointers. I’m just talking this out to see if my understanding here is correct.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:53:38+00:00Added an answer on June 9, 2026 at 2:53 pm

    Of course not.

    A method will only “stop executing” if it returns, if an exception is thrown, or if the thread is aborted.

    The whole point of garbage collection is that it is invisible to you. Barring special tricks (such as WeakReferences, or checking free memory), it is impossible to tell whether an object has been garbage collected – if you can check whether it exists, that means that you have a reference to it, so it cannot be collected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity that has a ManyToMany relationship with itself: @Entity public class
ive made a function that should remove a p-element if it doesnt have any
I have this monitor class that gives access to instances of itself with a
I want to delete/force garbage collection on a class instance from inside itself .
I have a HTML table like this: <table border=1> <tbody> <tr> <td><a href=# class=delete>DELETE
I have a HTML table like this: <table border=1> <tbody> <tr> <td><a href=# class=delete>DELETE
I have class that extend FragmentActivity in it I add fragment to layout as
I have class grades that I need to check if a specific grade is
I have one class ( A ) which knows when it should be deleted.
I have a Factory class that creates a Widget object. The Factory object needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.