I came across this little quine program, written without main method:
enum f {
f;
System z;
String s="enum f{f;System z;String s=%c%s%1$c;{z.out.printf(s,34,s);z.exit(0);}}";
{z.out.printf(s,34,s);
z.exit(0);}
}
Can somebody explain how does this work? Thanks.
Lines 5 and 6 are an instance initializer. It is called when the class is instantiated. Since this is an enum with one constant named
f, it is going to be instantiated once and the instance initializer block is executed.Note that
zisnull, butoutis a static member of classSystem, so you can callz.out.printf()anyway. Theprintfstatement takes the stringsas a format string with two arguments,34andsitself.34is the ASCII code for double quote". It is filled in for the%cand%1$cin the format string. The%sin the format string is replaced by the format stringsitself.