An article on classloading states that the method getClass() should not be called within a constructor because:
object initialization will be complete only at the exit of the constructor code.
The example they gave was:
public class MyClassLoader extends ClassLoader{
    public MyClassLoader(){
        super(getClass().getClassLoader()); // should not call getClass() because object
                                            //    initialization will be complete only at
                                            //    the exit of the constructor code.
    }
}
However from what I know, the native final method getClass() will always return the java.lang.Class object of that object instance, regardless of where it's called (within the constructor or not).
Will calling getClass() within the constructor ever give us problems?
If so, what are some examples whereby calling getClass() within the constructor would give us errors?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With