Demonstration of a simple constructor : Constructor « Class « Java






Demonstration of a simple constructor

Demonstration of a simple constructor
 

// : c04:SimpleConstructor.java
// Demonstration of a simple constructor.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

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();
  }
} ///:~


           
         
  








Related examples in the same category

1.Paying attention to exceptions in constructorsPaying attention to exceptions in constructors
2.Order of constructor callsOrder of constructor calls
3.Constructors and polymorphism don't produce what you might expectConstructors and polymorphism don't produce what you might expect
4.Constructor initialization with compositionConstructor initialization with composition
5.Constructors can have argumentsConstructors can have arguments
6.Show Constructors conflicting
7.Show that if your class has no constructors, your superclass constructors still get calledShow that if your class has no constructors, your superclass constructors still get called
8.Constructor calls during inheritanceConstructor calls during inheritance
9.A constructor for copying an object of the sameA constructor for copying an object of the same
10.Create a new instance of a class by calling a constructor with arguments