O'Reilly logo

Design Patterns Java™ Workbook by Steven John Metsker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Factory Method (Chapter 16)

SOLUTION 16.1Many answers are possible, but toString() is probably the most commonly used method that creates a new object. For example, the following code creates a new String object:
String s = new java.util.Date().toString();

The creation of strings often happens behind the scenes. Consider:

System.out.print(new Date());

This method creates a String object from the Date object, ultimately by calling the toString() method of class Date. It is interesting to walk through this in debugger, to see the details of what happens within a common print() method.

Another method that creates a new object is clone(). You may discover other methods that are “factories” for creating objects without necessarily exemplifying ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required