I'm not sure if the difference in loading static variables / blocks is between MyClass.classand Class.forName("MyClass"), for example, I have below class:
package test;
public class SampleClass{
public static SampleClass instance = new SampleClass();
private SampleClass(){
System.out.println("SampleClass Instance Created");
}
}
Then in another class, I accessed the class object above SampleClass using:
System.out.println(SampleClass.class);
Then the output will be:
class test.SampleClass
If I changed the usage class.forName()as shown below:
System.out.println(Class.forName("test.SampleClass"));
Then the output will be:
SampleClass Instance Created
class test.SampleClass
Can anyone give me an explanation? Many thanks.
source
share