O'Reilly logo

Java™ Puzzlers: Traps, Pitfalls, and Corner Cases by Neal Gafter, Joshua Bloch

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

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 ...

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