Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quine program without main()

Tags:

java

enums

quine

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.

like image 280
Uros K Avatar asked Feb 02 '12 22:02

Uros K


1 Answers

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 z is null, but out is a static member of class System, so you can call z.out.printf() anyway. The printf statement takes the string s as a format string with two arguments, 34 and s itself.

34 is the ASCII code for double quote ". It is filled in for the %c and %1$c in the format string. The %s in the format string is replaced by the format string s itself.

like image 76
Jesper Avatar answered Sep 20 '22 02:09

Jesper