When I execute the program and print the value of “this”, in both super class constructor as well as in child class constructor, the value of this (address location)…
System.out.println(this)与默认Object#toString的输出不是内存中实例的地址.它只是类的名称和实例的哈希码,没有更多.从the documentation:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@’, and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
This is typically implemented by converting the internal address of the object into an integer…
但它也说
…but this implementation technique is not required by the JavaTM programming language.
…当然,JVM可以根据需要自由地在内存中移动实例,但不允许更改hashCode.
…why dont I get the value of Test i.e, Test@someVal (Super class) when I print value of “this” in the super class.
有一个例子.该实例是子类.当您执行System.out.println(this)时,无论您是在基类还是子类中执行此操作,它仍然与您正在使用的对象实例相同.那个对象具有它从子类获得的特征,并且它还具有从超类继承的特征,但是没有两个单独的实例;有一个具有组合功能的实例.超级不是一个对象引用,虽然它看起来有点像一个;它是一种语法机制,专门要求编译器使用实例从超类继承的功能,而不是实例的等效功能(如果它们不同).