6. Classy Puzzlers

This chapter contains puzzlers that concern the use of classes and their instances, methods, and fields.

Puzzle 46: The Case of the Confusing Constructor

This puzzle presents you with two Confusing constructors. The main method invokes a constructor, but which one? The program’s output depends on the answer. What does the program print, or is it even legal?

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);    }}

Solution 46: Case of the Confusing Constructor

The parameter passed to the constructor is the ...

Get Java™ Puzzlers: Traps, Pitfalls, and Corner Cases now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.