1

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.

Gary Allen
  • 1,218
  • 1
  • 13
  • 28
  • Maybe this LINK answers your question: https://stackoverflow.com/questions/13126833/does-the-garbage-collector-work-on-static-variables-or-methods-in-java –  Jul 03 '20 at 10:14
  • Somewhat related: https://stackoverflow.com/q/171952/1639625 – tobias_k Jul 03 '20 at 10:15
  • Design wise - Have a GolfClub class which has a list of GolfMembers objects. It can have methods that add or remove from this list. The size of the lsit is your member count. I dont see the need for static variables. Any reason why u specifically need static? – leoOrion Jul 03 '20 at 10:19
  • maintain a [List](https://docs.oracle.com/javase/8/docs/api/java/util/List.html) of the instances of the class, representing member of a gold club. `List` has all the built-in methods that you need like adding an item in the list, removing an item from the list etc. Adding an instance to the `List` will increase its size and removing any instance will decrease its size. You can count the items in the `List` using [size()](https://docs.oracle.com/javase/8/docs/api/java/util/List.html#size--) function – Yousaf Jul 03 '20 at 10:20
  • Agree with @leoOrion. Don't depend on the garbage collector for this. Have your code call explicit `create` and `destroy` methods. Like you would do with, say, database connections. – Thilo Jul 03 '20 at 10:21
  • @leoOrion I agree that there are plenty of ways you could implement this, but I wanted to know how to do it using a static variable (just for curious sake :P) – Gary Allen Jul 03 '20 at 10:21
  • @Thilo I guess it is a bit of a weird example, in that calling "destroy" wouldn't really destroy the object, but would just relieve the member from the club. Maybe the question is **too** hypothetical and has no point really haha – Gary Allen Jul 03 '20 at 10:23
  • Are you asking how to access a static variable and mutate it ? – leoOrion Jul 03 '20 at 10:26
  • @leoOrion nope, I understand that – Gary Allen Jul 03 '20 at 10:31

1 Answers1

3

You could in theory use the finalize method to do something "on destruction", but that has several very real drawbacks and has fallen almost entirely out of favor.

Cleaners and PhantomReferences are modern replacements for final bit still not generally used for anything other than managing external resources (like open files, DB connections and the like).

So while there are technically feasible tools that can be used in a similar way to a C++ destructor in Java, they are not generally used in the same way.

Instead the lifetime of an object is considered virtually unlimited and its destruction is a cleanup task that's entirely decoupled from its actual job. In a sense that's actually required by Java, since you can never actively "destroy" an object. You can just stop using/referencing it.

So the Java-like way to handle something like that is to have a Club class that manages the members and creates them when requested in something like a method that looks like ClubMember joinPerson(Person person) and then has a void evictMember(ClubMember member) method that removes them from the club (and probably a HashMap somewhere).

Yes, the ClubMember object would still exist after the call to that method, but it could be marked as invalid in some way or maybe just no longer accepted by any of the Club method (by checking its presence in the internal HashMap before proceeding).

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 1
    Thanks! I understand you can't always reimplement something in the exact same way in one language as another language, so this was probably (unknowingly) the kind of answer I was looking for. Appreciate the insight – Gary Allen Jul 03 '20 at 10:42