定义仅出现一次的静态字段,并使用给定类的所有实例共享的单个值。
class StaticDemo { /* from 时代Java公众号 - nowjava.com */ public static boolean oneValueForAllObjects = false; } public class Main { public static void main (String[] args) { StaticDemo sd1 = new StaticDemo(); StaticDemo sd2 = new StaticDemo(); System.out.println(sd1.oneValueForAllObjects); System.out.println(sd2.oneValueForAllObjects); sd1.oneValueForAllObjects = true; System.out.println(sd1.oneValueForAllObjects); System.out.println(sd2.oneValueForAllObjects); /** 时代Java公众号 - nowjava.com **/ } }