Calling a Java method with no name

后端 未结 8 1430
迷失自我
迷失自我 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 02:45

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

    is an initialization block shared by the class(as indicated by static), which is executed first.

    {
            System.out.print("y ");
    
    }
    

    is an initialization block shared by all objects(constructors) of the class, which comes next.

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

    is a particular constructor for the class, which is executed third. The instance initialization block is invoked first every time the constructor is executed. That's why "y" comes just before "c".

    void go() {
            System.out.print("g ");
    }
    

    is just an instance method associated with objects constructed using the constructor above, which comes last.

提交回复
热议问题