Java Non-Denotable Types

You learn something new everyday as a programmer. Whilst reading a preview of The Well-Grounded Java Developer 2nd Edition (MEAP V02), I noticed something that looks like duck typing!

var duck = new Object() {
    void quack() {
        out.println("Quack!");
    }
};

duck.quack();

// output

Quack!

This is referred to as a non-denotable type, it is inferred as the type of the expression that is assigned. In this case, the type is effectively Object but extended with a method called quack().

Currently our duck variable can only be used in the local method, but perhaps this is a sign of more dynamic programming features to come? Interesting times.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s