[英]How do I figure out the identifier of an object in java?
I am writing some code and I am trying to use System.out.println()
to print the name of an object. 我正在编写一些代码,并且尝试使用System.out.println()
打印对象的名称。 For example, with the code String foo = new String("Hi");
例如,使用代码String foo = new String("Hi");
, I want to print out " foo
". ,我想打印出“ foo
”。 How do I do this? 我该怎么做呢?
You cannot do this, for multiple reasons. 由于多种原因,您无法执行此操作。 The simplest one is that one object can be referenced from multiple variables: 最简单的一个是可以从多个变量中引用一个对象:
String foo = new String("Hi");
String bar = foo;
Now both foo
and bar
refer to the same String
object "Hi"
. 现在, foo
和bar
引用相同的String
对象"Hi"
。 There is no way to decide on a single identifier. 无法确定单个标识符。
Names of local variables are, essentially, a compile-time artifact. 局部变量的名称实质上是一个编译时工件。 Once the compiler is done, you cannot access these names without access to the debug information produced when you compile with -g:vars
compiler flag . 编译器完成后,您将无法访问这些名称,而无法访问使用-g:vars
编译器标志进行编译时生成的调试信息。
您提供的对象引用名称不可能在运行时不可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.