1

Is it correct to say that a method is executed only once for a particular class?

Binhaoo
  • 33
  • 6
  • 4
    IIRC multiple class loaders can break this assumption. – kichik Feb 27 '20 at 18:21
  • 3
    A class static initializer is called each time a class is loaded. So I guess you are asking: How many times does (or can) a class get loaded. – Abra Feb 27 '20 at 18:26
  • 2
    @kichik If it was defined by a different ClassLoader, it’s a different class. A class named `com.example.Data` loaded by ClassLoader *C1* and a class named `com.example.Data` loaded by ClassLoader *C2* are different classes, and trying to cast an instance of one class to the other class will result in a ClassCastException. – VGR Feb 27 '20 at 18:32
  • [Loading a class twice in JVM using different loaders](https://stackoverflow.com/questions/14257357/loading-a-class-twice-in-jvm-using-different-loaders) – Abra Feb 27 '20 at 19:16
  • With multiple classloaders as kichik says, even though strictly speaking they would be different objects as VGR points out, you can't count on the initialization code being run exactly one time, so you at least have to keep that in mind when writing the init code. – Stephen P Feb 27 '20 at 19:21

1 Answers1

1

I have created Test class and referred it as follows

  • new operator
  • reflection

The < clinit > block got referred 1 time only

public class ClinitTest {

    public static void main(String[] args) throws ClassNotFoundException {
        Test b = new Test(); // initialization through NEW operator
        Class<Test> bcp2 =(Class<Test>) Class.forName("Test"); //initialisation through reflection
    }

}

class Test
{
    public static String name="Test";
    public Test()
    {
        System.out.println("In Constructor");
    }

    static
    {
        System.out.println("In Static");
    }

    {
        System.out.println("In Instance");
    }
}
Aditya Rewari
  • 2,343
  • 2
  • 23
  • 36
  • Instantiating directly with the `new` operator and again via reflection still uses only one [ClassLoader](https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html) as @kichik points out in the comments. With multiple classloaders, even though, strictly speaking they would be different objects as @VGR points out, you can't count on the initialization code being run exactly one time. – Stephen P Feb 27 '20 at 19:18
  • @StephenP As per my understanding..when multiple class loaders are involved, they have their work distributed. So, I do not suppose that, one class would be loaded by 2 different class loaders. Lets discuss this further please !!! What is your understanding over this ? – Aditya Rewari Feb 28 '20 at 10:13
  • an application can create new classloaders and load classes with them. ClassA loaded with the new classloader is technically a different class than ClassA loaded with the default classloader. Tomcat (and I assume others) uses this technique to re-load a running web application. You can, for example, fix a bug in SomeClass and drop the new .class file into the running application without re-deploying the whole application or taking Tomcat down. Tomcat will notice the update and load new instances of the classes in the app. – Stephen P Feb 28 '20 at 18:14
  • @StephenP So the example which you have mentioned above, is about HOT FIX / HOT DEPLOY property of servers ..am I correct ? – Aditya Rewari Mar 01 '20 at 07:04
  • yes, that is the example. As an example it shows that it _is_ possible and there _are_ cases where a class may be loaded by more than one classloader and, therefore, the clinit could be called more than once. – Stephen P Mar 02 '20 at 18:01