0

What does the below code do when you instantiate the StudentRegistrar class?

public class StudentRegistrar
{
    public StudentRegistrar ();
    {
        new RecordManager().Initialize();
    }
}

2 Answers 2

4

It doesn't necessary destroy the RecordManager immediately. Initialize() may spawn a new thread which can then hold a reference to the RecordManager. Since new threads are a garbage collection root, the RecordManager reference will be reachable from that root and therefore it will not be cleaned up.

It really depends on what Initialize does!

Sign up to request clarification or add additional context in comments.

Comments

1

It creates an instance of RecordManager, calls the Initialize() method and then destroys the instance of RecordManager.

EDIT: Actually it won't compile due to the spurious ;

3 Comments

destroys the instance of RecordManager should be replaced by makes the instance of RecordManager available for Garbage Collection
@Sean I agree that the word destroys is incorrect, however saying that it is made available for garbage collection makes a lot of assumptions and is only true if the initialize function doesn't create an object that maintains a strong or soft reference to the RecordManager instance. sjr's response is more technically accurate.
@LINEMAN78 sjr is making just as many assumptions. Either answer is correct, depending on who's assumptions are correct

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.