As part of the method m in class C, is not this.getClass() always C?
this.getClass()
No, it is not. If there are subclasses.
class C { Class m() { return this.getClass(); } } class D extends C { }
and then you can:
C c = new D(); cm(); // returns D.class
Nope
public class C { public void m() { System.out.println(this.getClass()); } } public class Child extends C {}
Then:
new Child().m(); // Prints Child
Not. Example:
public class Test { public static void main(String [] args) throws Exception { A a = new B(); a.reportThis(); } } class A { public void reportThis() { System.err.println(this.getClass().getName()); } } class B extends A { }
The this keyword refers to an object (an instance of a class) that is in scope. This means the instance that the method was called on, which in turn means that subclasses can also refer to the keyword 'this'.
Source: https://habr.com/ru/post/885404/More articles:Search SQL test suite generator, ideally open source - sqlPlaying back Ruby OpenSSL private_encrypt output in C # - c #Set f: parameter value using JavaScript - javaNumber of samples in the series Exceedence - c #How can I link to another project in Eclipse Java EE projects? - eclipseSpring cannot find class from Gradle shared library (dependency in autowired bean) when debugging in Eclipse - eclipseAES Encryption -Key Generation with OpenSSL - cSignature for TTabControl - delphiAndroid: vertical RatingBar? - androidHow to create a .pem file with aes key - cAll Articles