|
|
|
|
|
|
|
|
|
|
singleton
When several threads access a Singleton is has to be thread-safe and should cause as few locks as possible. Using lazy-loading, this can be achieved through double-checked locking (DCL). Though, in the literature you will find quite sophisticated articles explaining why DCL is not guaranteed by the JVM specs to work correctly.
download example
public class Singleton
singleton unit test
In order to test a Singleton for thread-safety you can start with the follwoing JUnit test case template. There is no guarantee for correctness, though.
download example
public void testThread()
read from and write to classpath
Sometimes it is necessary to read from a file on the classpath or write to a file that is already on the classpath.
download example
// read from classpath
array casting
In some situation elements of the same class are added to a collection. The collection's elements can then be retrieved as an array of the elements' class (and not only as an array of Object).
download example
fList = new Vector();
reflection
Reflection is very powerful in Java. It allows at runtime to retrieve all kind of information about a class like its methods, its super class, etc...
download example
Vector vector = new Vector();
In addition to retrieving information about a class, it is possible to invoke methods, instantiate classes, and change field values.
download example
myClass = Class.forName("java.lang.Integer");
There is even the possibility to access fields, constructors, and methods that have only private access.
download example
// the method getIterator has only private access
execute external applications
There is the possibility to execute external applications from within your Java application and wait for the executed application to finish before continuing with your Java program.
download example
Process p = Runtime.getRuntime().exec("notepad");
find location of class file
Sometimes it helps to know the location from where a class file has been loaded.
download example
Class c = "foo".getClass();
serializing into and deserializing from string
An object can be serialized into a string and later be deserialized from that string again. Compression is also easy to include in order to keep the string shorter.
download example
// serialize object into a string
serializing into and deserializing from pseudo.xml
There is a library called Java Serialization for XML (JSX) that allows to serialize any object into pseudo-XML and later deserialize it from pseudo-XML again.
download example
// serialize object into a string
number crunching
In high school I often forgot to do my homework and my math teacher always made calculate twenty to the power of four. If there had been Java back then...
download example
// not exact enough
swing look & feel
You can define the default look & feel of your Swing application on the command line by setting a system property.
java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel MyApplication
|