New to Java. I want to understand how to correctly, but at the same time, simply list all the instances of the class and display their names. For what it is necessary, don’t ask, I don’t know myself, it’s just my thoughts that brought me to the question: “can it even be done at all?”. If this is prohibited by the concept of OOP in Java, then I would like a corresponding comment. In BlueJ, all created instances of classes are always displayed, so it’s possible! How to calculate instances is clear: It is necessary to increase the static variable in the constructor by 1. But how beautifully to write to the List the names of the instances? Or is there a stupid way for me to list them?
2 answers
I would recommend to continue reading, and not be distracted by any useless nonsense :-) There are many more interesting things in the Java world :-)
You will not get the names of variables declared in a method. A variable is just a pointer to an object. And to say, in the context of binding an object to a variable name is incorrect. (By the way, the concept of "Имя экземпляра класса" does not exist in Java)
That is, for such a case:
public void main(String args[]) { Test t1 = new Test(); ... } it is impossible to determine the name of the variable t1 .
If we talk about the list of instances of a particular class, and not about their "names", then there is another conversation.
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 Here the author advises to watch the source code IDE Eclipse using the Java Debug Wire Protocol and draws our attention to the Java Debug Interface .
If we are interested in a particular class, then you can use this solution: https://stackoverflow.com/questions/10071065/getting-all-instances-of-a-class
true, I would simplify the code like this:
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)); } } That is, now, having addressed Test.instances we will receive the list of weak links to all copies of this class. Why WeakReference !? In order not to interfere with the garbage collector to clean them. If we just made a list of objects, the garbage collector would never remove these objects, even if they are not already used in the code.
through reflection, perform Instanceof, if it matches, then ++ for example, this is how you can get all the fields
/* Generate column list from fields of class type */ public String getColumns(Class classType){ String result = ""; Field[] classFields = classType.getFields(); for(int i=0; i< classFields.length-1; i++) result += classFields[i].getName() + ", "; result += classFields[classFields.length-1].getName(); return result; } public void fieldValue(Class classType){ Object typeOfFieldsClass = classType.newInstance(); Field[] classFields = typeOfFieldsClass.getClass().getFields(); for (Field field : classFields) { field.setAccessible(true); field.get(someObject) // и вот тут получаешь значение - and how through reflection to check all the fields? I understand one field, I can imagine all the fields of a class, but how is everything? (Including maybe variables in anonymous classes or local or anonymous variables) ... - pavel
- @pavel, corrected the answer - Senior Pomidor
- wait for the author, I understood the question a little differently. - pavel
- Immediately warn the author will be a long time to eat. - user2931517
- @ user2931517, it seems everything is humanly written. but if that - ask - Senior Pomidor
int a = new Test().getInt();and then what? All the names you will not know, the hash codes can still be ... - pavelunsafe), which is against the methodology ... - pavel