Java: What is the difference between and ?

前端 未结 4 1830
不知归路
不知归路 2020-12-02 08:11

I am unable to understand the following text... Does it mean that is for empty constructors? Why is important to have two different versions?

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 08:57

    Just to add If you use Class.forName method, it only intializes the class. So from within this method, it makes a call only to clinit and when you use newInstance on the object returned from forName, it will call init for the instance initialization. You can use below code to see it in debug.

    public class ByteCodeParent
    {
     public static String name="ByteCode";
     public ByteCodeParent()
    {
        System.out.println("In Constructor");
    }
    
     static
     {
         System.out.println("In Static");
     }
    
     {
         System.out.println("In Instance");
     }
    

    To test, use

       Class bcp2 =(Class) Class.forName("ByteCodeParent");
    ByteCodeParent bcp4= bcp2.newInstance();
    

提交回复
热议问题