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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.