As programming languages grow, it is
inevitable that hidden features begin to appear and constructs that were
never intended by the founders begin to creep into common usage. Some
of these features rear their head as idioms and become accepted parlance
in the language, while others become anti-patterns and are relegated to
the dark corners of the language community. In this article, we will
take a look at five Java secrets that are often overlooked by the large
population of Java developers (some for good reason). With each
description, we will look at the use cases and rationale that brought
each feature into the existence and look at some examples when it may be
appropriate to use these features.
The reader should note that not all these features are not
truly hidden in the language, but are often unused in daily programming.
While some may be very useful at appropriate times, others are almost
always a poor idea and are shown in this article to peek the interest of
the reader (and possibly give him or her a good laugh). The reader
should use his or her judgment when deciding when to use the features
described in this article: Just because it can be done does not mean it
should.
1. Annotation Implementation
Since Java Development Kit (JDK) 5, annotations have an
integral part of many Java applications and frameworks. In a vast
majority of cases, annotations are applied to language constructs, such
as classes, fields, methods, etc., but there is another case in which
annotations can be applied: As implementable interfaces. For example,
suppose we have the following annotation definition:
Normally, we would apply this annotation to a method, as in the following: