||Calling Methods from a Constructor
In this newsletter we investigate what can go wrong when we call methods from constructors, showing examples from the JDK, Glassfish, Spring Framework and some other well known frameworks..
||Throwing Exceptions from Fields
How can you set a field at point of declaration if its constructor throws a checked exception?
||Final Parameters and Local Variables
The trend of marking parameters and local variables as "final" does not really enhance your code, nor does it make it more secure.
||GOTO in Java
It is possible to use the break statement to jump out to the end of a labelled scope, resulting in some strange looking code, almost like the GOTO statement in C.
||Hacking Java Surreptitiously
Surreptitious: stealthy, furtive, well hidden, covert. In this newsletter we will show two Java puzzles written by Wouter Coekaerts that require a surreptitious solution. You cannot do anything to upset the security manager.
||Pushing the Limits in Java's Random
What is the largest double that could in theory be produced by Math.random()? In this newsletter, we look at ways to calculate this based on the 48-bit random generator available in standard Java. We also prove why in a single-threaded program, (int)(Random.nextDouble() + 1) can never be rounded up to 2.
||What is the Meaning of Life?
In this newsletter we try to calculate the meaning of life, with surprising results.
||Automatically Unlocking with Java 7 - Follow-up
In this newsletter we discuss why we unfortunately will not be able to use the try-with-resource mechanism to automatically unlock in Java 7.
||Automatically Unlocking with Java 7
In this newsletter we explore my favourite new Java 7 feature "try-with-resource" and see how we can use this mechanism to automatically unlock Java 5 locks.
Most of the northern hemisphere is on holiday, so here is a quick quiz for those poor souls left behind manning the email desk. How can we prevent a ConcurrentModificationException in the iterator?
Generics can be used to further improve the WalkingCollection, shown in our previous newsletter.
||Logging Part 3 of 3
After almost nine years of silence, we come back to bring the logging series to an end, looking at best practices and what performance measurements to log.
In this newsletter we show the reflection plumbing needed for writing a socket monitor that sniffs all the bytes being sent or received over all the Java sockets. The Delegator is used to invoke corresponding methods through some elegant guesswork.
||Annotation Processing Tool
In this newsletter we answer the question: "How do we force all subclasses to contain a public no-args constructor?" The Annotation Processing Tool allows us to check conditions like this at compile time, rather than only at runtime.
||Of Hacking Enums and Modifying "final static" Fields
The developers of the Java language tried their best to stop us from constructing our own enum instances. However, for testing purposes, it can be useful to temporarily add new enum instances to the system. In this newsletter we show how we can do this using the classes in sun.reflect. In addition, we use a similar technique to modify static final fields, which we need to do if we want the switch statements to still work with our new enums.
Enums are implemented as constant flyweights. You cannot construct them. You cannot clone them. You cannot make copies with serialization. But here is a way we can make new ones in Java 5.
||Casting like a Tiger
Java 5 adds a new way of casting that does not show compiler warnings or errors. Yet another way to shoot yourself in the foot?
When we make proxies that wrap objects, we have to remember to write an appropriate equals() method. Instead of comparing on object level, we need to either compare on interface level or use a workaround to achieve the comparisons on the object level, described in this newsletter.
||Strategy Pattern with Generics
The Strategy Pattern is elegant in its simplicity. With this pattern, we should try to convert intrinsic state to extrinsic, to allow sharing of strategy objects. It gets tricky when each strategy object needs a different set of information in order to do its work. In this newsletter, we look at how we can use Java 5 Generics to pass the correct subtype of the context into each strategy object.
||How Deep is Your Hierarchy?
Someone asked me yesterday what the maximum inheritance depth is in Java. I guessed a value of 65535, but for practical purposes, not more than 5. When I asked performance guru Kirk Pepperdine to estimate, he shot back with 63. In this newsletter, we look at the limitations in the JVM and examine some existing classes.
||Exceptions From Constructors
What do you do when an object cannot be properly constructed? In this newsletter, we look at a few options that are used and discuss what would be best. Based on the experiences of writing the Sun Certified Programmer for Java 5 exam.
||Reflectively Calling Inner Class Methods
Sometimes frameworks use reflection to call
methods. Depending how they find the correct method to
call, we may end up with IllegalAccessExceptions. The
naive approach of clazz.getMethod(name) is not correct
when we send instances of non-public classes.
||Compile-time String Constant Quiz
When we change libraries, we need to do a full recompile of our code, in
case any constants were inlined by the compiler. Find out which constants
are inlined in this latest newsletter.
||Enum Inversion Problem
A problem that I encountered when I first started using enums was how
to serialize them to some persistent store. My initial approach was to
write the ordinal to the database. In this newsletter, I explore some
ideas of a more robust approach. It will also show you some applications
of Java generics.
||Break to Labeled Statement
||Strategy Pattern of HashCode Equality
||Object Adapter based on Dynamic Proxy
||Making Enumerations Iterable
||New for/in loop gymnastics
||Mapping Objects to XML Files using Java 5 Annotations
||Java 5 - "final" is not final anymore
||Follow-up: Self-reloading XML Property Files
||Self-reloading XML Property Files
||Java Field Initialisation
||Ego Tripping with Webservices
||End of Year Puzzle
||End of Year Puzzle Follow-up
||Many Public Classes in One File
||LinkedHashMap is Actually Quite Useful
||Java and Dilbert
||Overloading considered Harmful
||Revisiting Stack Trace Decoding
||Follow-up and Happy New Year!
||The link to the outer class
||Shutting down threads cleanly
||Once upon an Oak ...
||Commenting out your code?
||Lack of Streaming leads to Screaming
||Arrgh, someone wants to kill me!
||Visiting your Collection's Elements
||Using Unicode Variable Names
||Generic Types with Dynamic Decorators
||Exceptional Constructors - Resurrecting the dead
||Hash, hash, away it goes!
When I first started using Java in 1997, I needed very large hash tables for matching records as quickly as possible. We ran into trouble when some of the keys were mutable and ended up disappearing from the table, and then reappearing again later.
||Multicasting in Java
||Classloaders Revisited: "Hotdeploy"
||Non-virtual Methods in Java
||Serializing Objects Into Database
||Class names don't identify a class
||Switching on Object Handles
||Hooking into the shutdown call
||Implementation code inside interfaces
||Dynamic Proxies - Short Tutorial
||Logging part 2
||Logging part 1
||Anonymous Inner Classes