Can I get the name of the instance of a class?


I do not know if the question is well written but this is the question.

I have my class ProductosLacteos . When I create the instance I do this:

ProductosLacteos quesito = new ProductosLacteos();

Then I want to get quesito as a text string. For what? Question of debugging. I have a console where different program data are shown, but the plan is that within ProductosLacteos you can access the name of the instance with getNombreDeLaInstancia() . This is what I hope:

ProductosLacteos quesito = new ProductosLacteos();
ProductosLacteos lechita = new ProductosLacteos();
ProductosLacteos jocoque = new ProductosLacteos();


And I get results:



Is it possible to access this data with extends so that you have it available for all my classes?

Thank you!

asked by Angel 26.04.2017 в 20:01

3 answers


As of Java 8 you can make use of the reflection.


  • import java.lang.reflect.Field;
  • .getName()
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;

import java.lang.reflect.Field;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
    public String quesito = new String ("test");
    public String lechita = new String ("test");
    public String lechita = new String ("test");

    public static void main (String[] args) throws java.lang.Exception, java.lang.IllegalArgumentException, java.lang.IllegalAccessException

     Ideone t = new Ideone();
     for(Field f : t.getClass().getFields()) {

      System.out.println("nombre instancia: " + f.getName());

Note: a String is used because I do not have that class, and to not make the example longer, the class String is used because it is the simulation of yours, but it should work the same.



answered by 02.05.2017 / 18:30

The instance does not have a "name".

What you see is a reference , which directs you to what is the instance itself. But the name of the reference is only so you can use it to compile your program (instead of, for example, using the value of the stackpointer plus a scroll). For example, the compiled code does not keep the name of the local variables, it is completely unknown.

To illustrate, think about what you do:

quesito = lechita;

Now the two references point to the same instance. What is the "name" of this one?

If you want to identify an instance of a class by an attribute, include that attribute within the class and give it a value. For example:

public class ProductoLacteo { //Nota: en singular, porque cada instancia representa un producto
  private String nombre;
  public ProductoLacte(String nombre) {
    this.nombre = nombre;

ProductoLacteo lechita = new ProductoLacteo("Lechita");

Now you can get the "name" by accessing the property (using a getter ). You can also reimplement toString , and perhaps equals and hashCode , relative to it.

answered by 26.04.2017 в 20:12

The name of the variable is only the identifier of the memory space, the instance of an object does not have a name in itself, it could have a property but it would not work for your idea unless the property stores the variable name of the object .

answered by 27.04.2017 в 00:30