Your preferences have been saved!
» Create a new instance of a class by calling a - import java.lang.reflect.Constructor; /**  * Handy reflection routines.  */ public abstract class Reflect {   /**    * Create a new instance of a class by calling a constructor with arguments.    */   public static Object newInstance(String className, Class[] signature, Object[] args)       throws Exception {... 17 Feb 12 » A constructor for copying an object of the same - import java.lang.reflect.Constructor; class FruitQualities {   private int weight;   private int color;   private int firmness;   private int ripeness;   private int smell;   // etc.   public FruitQualities() { // Default constructor     // Do something meaningful...... 17 Feb 12 » Constructor calls during inheritance - class Art {   Art() {     System.out.println("Art constructor");   } } class Drawing extends Art {   Drawing() {     System.out.println("Drawing constructor");   } }... 17 Feb 12 » Show that if your class has no constructors, your superclass - /** Show that if your class has no constructors, your superclass'  * constructors still get called.  */ public class InheritConstructor extends SomeOtherClass {   public static void main(String[] c) {     new InheritConstructor().run();   }   public void run() {     System.out.println("In InheritConstructor::run");   }... 17 Feb 12 » Show Constructors conflicting - /** Show Constructors conflicting */ public class Constructors {   /** Constructor */   public Constructors() {     System.out.println("In the constructor");   }   /** Constructor that throws */   public Constructors(int value) {     if (value < 0)       throw new IllegalArgumentException("Constructors: value < 0");... 17 Feb 12 » Constructors can have arguments - class Rock2 {   Rock2(int i) {     System.out.println("Creating Rock number " + i);   } } public class SimpleConstructor2 {   public static void main(String[] args) {     for (int i = 0; i < 10; i++)       new Rock2(i);   }... 17 Feb 12 » Demonstration of a simple constructor - class Rock {   Rock() { // This is the constructor     System.out.println("Creating Rock");   } } public class SimpleConstructor {   public static void main(String[] args) {     for (int i = 0; i < 10; i++)       new Rock();   }... 17 Feb 12 » Constructor initialization with composition - class Soap {   private String s;   Soap() {     System.out.println("Soap()");     s = new String("Constructed");   }   public String toString() {     return s;   } }... 17 Feb 12 » Constructors and polymorphism don't produce what you might expect - abstract class Glyph {   abstract void draw();   Glyph() {     System.out.println("Glyph() before draw()");     draw();     System.out.println("Glyph() after draw()");   } } class RoundGlyph extends Glyph {   private int radius = 1;... 17 Feb 12 » Order of constructor calls - class Meal {   Meal() {     System.out.println("Meal()");   } } class Bread {   Bread() {     System.out.println("Bread()");   } }... 17 Feb 12