What is the output of the following code?
import com.java2s.*; import static com.java2s.Line.*; public class Main { private static Line l1 = new Line(); private static Line l2 = new Line(); { System.out.println(l1.length); } public static void main(String[] args) { l1.length = 2; l2.length = 8; System.out.println(l1.length); } } package com.java2s; public class Line { public static int length = 0; }
D.
Main has an instance initializer and not a static initializer.
Since Main is never constructed, the instance initializer does not run.
The other detail is that length is static.
Changes from one object update this common static variable.