How do you list all the copies of the class?



  • Java. I want to understand how right it is, but at the same time, simply list all the copies of the class and remove their names. Why don't you ask me, I don't know, just my thoughts have come to the question, "Can you even do it?" If this was prohibited by the PLO concept in Java, a comment would be welcome. BlueJ always produces all the created copies of classes, which means you can! How do you read the copies? The designer needs a variable static increase by 1. Is that how it's nice to put the names on the list? Or is there a stupid simple mechanism for me to list them?



  • I'd recommend that we continue reading, and not distracting all the useless nonsense. There are many more interesting things in the Java world:

    The names of variables declared in some method will not be obtained. Reassignment is just an indicator on the object. And speaking, in the context of linking the object to the name of the variable, it is not correct. "Имя экземпляра класса" Java does not exist)

    I mean, for this case:

    public void main(String args[]) {
      Test t1 = new Test();
      ...
    }
    

    define the name of the variable t1 Impossible.

    Well, speaking of a list of certain classes, not their names, there's another conversation.

    In the English version of StackOverflow already answered this question:

    https://stackoverflow.com/questions/1947122/is-there-a-simple-way-of-obtaining-all-object-instances-of-a-specific-class-in-j This is where the author advises to watch the references IDE Eclipse. Java Debug Wire Protocol and draws our attention Java Debug Interface

    If we're interested in a particular class, we can use this solution: https://stackoverflow.com/questions/10071065/getting-all-instances-of-a-class

    I'd simplify the code so:

    public class Test {
       public static List<java.lang.ref.WeakReference<Test>> instances = new ArrayList<>();
    

    public Test() {
    instances.add(new java.lang.ref.WeakReference<Test>(this));
    }
    }

    I mean, now, when you turn. Test.instances We'll get a list of weak references to all copies of this class. Why? WeakReference ! To prevent the garbage collector from cleaning them. If we had just made a list of objects, the garbage collector would never have taken these objects, even if they were no longer used in the code.


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2