简体   繁体   English

如何找出Java中对象的标识符?

[英]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" . 现在, foobar引用相同的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.

相关问题 如何确定应用程序中的特定对象已不再使用? - How do I figure out that a particular object in an application is no longer in use? 弄清楚我如何获得Java包 - figure out how I got a Java package 如何确定需要从Java库导入的命名空间? - How do I figure out what namespace I need to import from a java library? 我正在netbeans上运行基于Java的游戏。 如何计算其中的数据流? - I am running a java based game on netbeans . How do I figure out the data flow in it? 我如何找出持有锁并阻塞我的Java线程的东西? - How do I figure out what's holding a lock and blocking my Java thread? 我如何找出并更改 Java Maven 正在使用哪个版本执行? - How do I figure out and change which version of Java Maven is using to execute? 无法找出 Spring-boot 中相同标识符值的 object - Unable to figure out the object in same identifier value in Spring-boot 根据教授需要弄清楚如何操纵变异子以找出一个类,我对此感到困惑 - need to figure out how to manipulate the mutator to figure out a class according to professor, I am confusd on how to do that 如何判断一个对象是否是向量 - how to figure out if an object is a vector 如何确定我在哪里单击 JFrame? - How do I figure out where I am clicking on a JFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM