//: initialization/SimpleConstructor.java// Demonstration of a simple constructor.classRock{Rock(){// This is the constructorSystem.out.print("Rock ");}}publicclassSimpleConstructor{publicstaticvoidmain(String[]args){for(inti=0;i<10;i++)newRock();}}/* Output:Rock Rock Rock Rock Rock Rock Rock Rock Rock Rock*///:~
//: initialization/SimpleConstructor2.java// Constructors can have arguments.classRock2{Rock2(inti){System.out.print("Rock "+i+" ");}}publicclassSimpleConstructor2{publicstaticvoidmain(String[]args){for(inti=0;i<8;i++)newRock2(i);}}/* Output:Rock 0 Rock 1 Rock 2 Rock 3 Rock 4 Rock 5 Rock 6 Rock 7*///:~
//: initialization/Overloading.java// Demonstration of both constructor// and ordinary method overloading.import staticnet.mindview.util.Print.*;classTree{intheight;Tree(){print("Planting a seedling");height=0;}Tree(intinitialHeight){height=initialHeight;print("Creating new Tree that is "+height+" feet tall");}voidinfo(){print("Tree is "+height+" feet tall");}voidinfo(Strings){print(s+": Tree is "+height+" feet tall");}}publicclassOverloading{publicstaticvoidmain(String[]args){for(inti=0;i<5;i++){Treet=newTree(i);t.info();t.info("overloaded method");}// Overloaded constructor:newTree();}}/* Output:Creating new Tree that is 0 feet tallTree is 0 feet talloverloaded method: Tree is 0 feet tallCreating new Tree that is 1 feet tallTree is 1 feet talloverloaded method: Tree is 1 feet tallCreating new Tree that is 2 feet tallTree is 2 feet talloverloaded method: Tree is 2 feet tallCreating new Tree that is 3 feet tallTree is 3 feet talloverloaded method: Tree is 3 feet tallCreating new Tree that is 4 feet tallTree is 4 feet talloverloaded method: Tree is 4 feet tallPlanting a seedling*///:~
//: initialization/OverloadingOrder.java// Overloading based on the order of the arguments.import staticnet.mindview.util.Print.*;publicclassOverloadingOrder{staticvoidf(Strings,inti){print("String: "+s+", int: "+i);}staticvoidf(inti,Strings){print("int: "+i+", String: "+s);}publicstaticvoidmain(String[]args){f("String first",11);f(99,"Int first");}}/* Output:String: String first, int: 11int: 99, String: Int first*///:~
//: initialization/NoSynthesis.javaclassBird2{Bird2(inti){}Bird2(doubled){}}publicclassNoSynthesis{publicstaticvoidmain(String[]args){//! Bird2 b = new Bird2(); // No defaultBird2b2=newBird2(1);Bird2b3=newBird2(1.0);}}///:~
//: initialization/Leaf.java// Simple use of the "this" keyword.publicclassLeaf{inti=0;Leafincrement(){i++;returnthis;}voidprint(){System.out.println("i = "+i);}publicstaticvoidmain(String[]args){Leafx=newLeaf();x.increment().increment().increment().print();}}/* Output:i = 3*///:~
//: initialization/Flower.java// Calling constructors with "this"import staticnet.mindview.util.Print.*;publicclassFlower{intpetalCount=0;Strings="initial value";Flower(intpetals){petalCount=petals;print("Constructor w/ int arg only, petalCount= "+petalCount);}Flower(Stringss){print("Constructor w/ String arg only, s = "+ss);s=ss;}Flower(Strings,intpetals){this(petals);//! this(s); // Can't call two!this.s=s;// Another use of "this"print("String & int args");}Flower(){this("hi",47);print("default constructor (no args)");}voidprintPetalCount(){//! this(11); // Not inside non-constructor!print("petalCount = "+petalCount+" s = "+s);}publicstaticvoidmain(String[]args){Flowerx=newFlower();x.printPetalCount();}}/* Output:Constructor w/ int arg only, petalCount= 47String & int argsdefault constructor (no args)petalCount = 47 s = hi*///:~
//: initialization/OrderOfInitialization.java// Demonstrates initialization order.import staticnet.mindview.util.Print.*;// When the constructor is called to create a// Window object, you'll see a message:classWindow{Window(intmarker){print("Window("+marker+")");}}classHouse{Windoww1=newWindow(1);// Before constructorHouse(){// Show that we're in the constructor:print("House()");w3=newWindow(33);// Reinitialize w3}Windoww2=newWindow(2);// After constructorvoidf(){print("f()");}Windoww3=newWindow(3);// At end}publicclassOrderOfInitialization{publicstaticvoidmain(String[]args){Househ=newHouse();h.f();// Shows that construction is done}}/* Output:Window(1)Window(2)Window(3)House()Window(33)f()*///:~
//: initialization/StaticInitialization.java// Specifying initial values in a class definition.import staticnet.mindview.util.Print.*;classBowl{Bowl(intmarker){print("Bowl("+marker+")");}voidf1(intmarker){print("f1("+marker+")");}}classTable{staticBowlbowl1=newBowl(1);Table(){print("Table()");bowl2.f1(1);}voidf2(intmarker){print("f2("+marker+")");}staticBowlbowl2=newBowl(2);}classCupboard{Bowlbowl3=newBowl(3);staticBowlbowl4=newBowl(4);Cupboard(){print("Cupboard()");bowl4.f1(2);}voidf3(intmarker){print("f3("+marker+")");}staticBowlbowl5=newBowl(5);}publicclassStaticInitialization{publicstaticvoidmain(String[]args){print("Creating new Cupboard() in main");newCupboard();print("Creating new Cupboard() in main");newCupboard();table.f2(1);cupboard.f3(1);}staticTabletable=newTable();staticCupboardcupboard=newCupboard();}/* Output:Bowl(1)Bowl(2)Table()f1(1)Bowl(4)Bowl(5)Bowl(3)Cupboard()f1(2)Creating new Cupboard() in mainBowl(3)Cupboard()f1(2)Creating new Cupboard() in mainBowl(3)Cupboard()f1(2)f2(1)f3(1)*///:~
//: initialization/NewVarArgs.java// Using array syntax to create variable argument lists.publicclassNewVarArgs{staticvoidprintArray(Object...args){for(Objectobj:args)System.out.print(obj+" ");System.out.println();}publicstaticvoidmain(String[]args){// Can take individual elements:printArray(newInteger(47),newFloat(3.14),newDouble(11.11));printArray(47,3.14F,11.11);printArray("one","two","three");printArray(newA(),newA(),newA());// Or an array:printArray((Object[])newInteger[]{1,2,3,4});printArray();// Empty list is OK}}/* Output: (75% match)47 3.14 11.1147 3.14 11.11one two threeA@1bab50a A@c3c749 A@150bd4d1 2 3 4*///:~