本帖最后由 jacktse1983 于 2009-07-24 15:07:19 编辑

解决方案 »

  1.   


    public class ex2 {
    double f;
    String s; public void print(double x) {
    ex2 exs = new ex2();
    exs.s = exs.getClass().getSimpleName();//就是这的问题 
    System.out.println("The value of " + exs.s + " is:" + x);
    }
    }
      

  2.   


    谢谢LS的,但是这样只能得到实例所引用的类名,得不到当前的实例名,看了一个别的论坛的帖子,有人问到类似的问题,貌似也没有解决方法,因为每次编译后得到的名字只是地址,名字不专门做存储.不知对否...
    "I'm afraid, that there is no way to do this. The name "xxx" is no name at
    all. It is just an identifier you named for the compiler. When it get's
    compiled every access to xxx is substituted with the objects memory
    address." 
      

  3.   

    I'm afraid, that there is no way to do this
      

  4.   

    只能得到类名,没有实例名,新创建对象本质是分配一块地址的。你可以得到这个对象的HASHCODE
      

  5.   

    同意,我试过了toString(),得到的是类名和对象的hash code,我想可能编译器在编译的时候由于java特有的动态性一旦创建实例就全转换为内存地址了,并无分配空间来存储实例名,感觉这样有点麻烦...
    谢谢各位!