Calling a Java method with no name

后端 未结 8 1434
迷失自我
迷失自我 2020-11-28 02:34

I\'m looking at the code below and found something a bit strange:

public class Sequence {
    Sequence() {
        System.out.print(\"c \");
    }

    {
            


        
8条回答
  •  南笙
    南笙 (楼主)
    2020-11-28 02:35

    static {
        System.out.print("x ");
    }
    

    Static blocks are only executed once when the class is loaded and initialized by the JRE.

    And non-static block will be call every time your are creating a new instance and it will be call just before the Constructor.

    As here you've created only 1 instance of Sequence so constructed has been called after non-static blocks and then the method which actually your goal.

提交回复
热议问题