I come from a C++ background where I am used to managing memory myself. I do understand that Java's garbage collector will do this for me. I am asking how I would do the following.
Let's say I have a class which represents a member of a golf club. It simply stores their name. This situation is obviously hypothetical as you'd probably store all members in some database, but let's say everything is created statically by the programmer themselves and is stored in memory.
If I have a static variable for this class, say "memberCount", I can easily increase this when a new object instance is created.
My question is, how/when would I decrease this variable when an object is destroyed? And from the user of the class's perspective, how would I design this so that they could call some sort of "memberHasLeft" method to indicate that the object can be freed and the memberCount should decrease?
I know this is a bit of a weird example, but hypothetically, how would you accomplish this?
Edit: Yes, I would never design something this way. But if this sort of scenario were to arise in C++, I would simply be able to increment the static variable in the constructor, and decrement it in the destructor. I am looking for the most-alike Java equivalent.