public class Confusing { private Confusing(Object o) { System.out.println("Object"); } private Confusing(double[] dArray) { System.out.println("double array"); } public static void main(String[] args) { new Confusing(null); } }
class Counter { private static int count = 0; public static final synchronized void increment() { count++; } public static final synchronized int getCount() { return count; } } class Dog extends Counter { public Dog() { } public void woof() { increment(); } } class Cat extends Counter { public Cat() { } public void meow() { increment(); } } public class Ruckus { public static void main(String[] args) { Dog dogs[] = { new Dog(), new Dog() }; for (int i = 0; i < dogs.length; i++) dogs[i].woof(); Cat cats[] = { new Cat(), new Cat(), new Cat() }; for (int i = 0; i < cats.length; i++) cats[i].meow(); System.out.print(Dog.getCount() + " woofs and "); System.out.println(Cat.getCount() + " meows"); } }
class Dog { public static void bark() { System.out.print("woof "); } } class Basenji extends Dog { public static void bark() { } } public class Bark { public static void main(String args[]) { Dog woofer = new Dog(); Dog nipper = new Basenji(); woofer.bark(); nipper.bark(); } }
import java.util.Calendar; public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static final int CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR); private Elvis() { beltSize = CURRENT_YEAR - 1930; } public int beltSize() { return beltSize; } public static void main(String[] args) { System.out.println("Elvis wears a size " + INSTANCE.beltSize() + " belt."); } }
public class Type1 { public static void main(String[] args) { String s = null; System.out.println(s instanceof String); } } public class Type2 { public static void main(String[] args) { System.out.println(new Type2() instanceof String); } } public class Type3 { public static void main(String args[]) { Type3 t2 = (Type3) new Object(); } }
class Point { protected final int x, y; private final String name; // Cached at construction time Point(int x, int y) { this.x = x; this.y = y; name = makeName(); } protected String makeName() { return "[" + x + "," + y + "]"; } public final String toString() { return name; } } public class ColorPoint extends Point { private final String color; ColorPoint(int x, int y, String color) { super(x, y); this.color = color; } protected String makeName() { return super.makeName() + ":" + color; } public static void main(String[] args) { System.out.println(new ColorPoint(4, 2, "purple")); } }
class Cache { static { initializeIfNecessary(); } private static int sum; public static int getSum() { initializeIfNecessary(); return sum; } private static boolean initialized = false; private static synchronized void initializeIfNecessary() { if (!initialized) { for (int i = 0; i < 100; i++) sum += i; initialized = true; } } } public class Client { public static void main(String[] args) { System.out.println(Cache.getSum()); } }
// This is meant to represent a library class. You must not modify it. public class Thing { public Thing(int i) { } } public class MyThing extends Thing { private final int arg; /* * This constructor is illegal. Rewrite it so that it has the same * effect but is legal. */ public MyThing() { super(arg = (int)System.currentTimeMillis()); } }
public class Null { public static void greet() { System.out.println("Hello world!"); } public static void main(String[] args) { ((Null) null).greet(); } }
public class Creator { public static void main(String[] args) { for (int i = 0; i < 100; i++) Creature creature = new Creature(); System.out.println(Creature.numCreated()); } } class Creature { private static long numCreated = 0; public Creature() { numCreated++; } public static long numCreated() { return numCreated; } }
No comments:
Post a Comment