javaworld

Back to your Class roots

July 11, 2003Q: If I have an instance of class X, how can I figure out its classfile location at runtime?A: Before I give my answer, I want to point out that your life with Java will be much easier if you get into the habit of writing strictly disk location-independent code. Load resources such as property and configuration files via Class.getResource() and ResourceBundle.getBundle() as much as possible and avoid java.util.File until...

javaworld

Into the mist of serialization myths

June 27, 2003Q: Does setting the serialVersionUID class field improve Java serialization performance?A: I have heard this performance "tip" from several colleagues and keep running across it in various Java forums. In this Java Q&A installment, we discover that it is mostly an urban legend.[ Learn Java from beginning concepts to advanced design patterns in this comprehensive 12-part course! ]The question refers to the following...

javaworld

Find a way out of the ClassLoader maze

June 6, 2003Q: When should I use Thread.getContextClassLoader() ?A: Although not frequently asked, this question is rather tough to correctly answer. It usually comes up during framework programming, when a good deal of dynamic class and resource loading goes on. In general, when loading a resource dynamically, you can choose from at least three classloaders: the system (also referred to as the application) classloader, the current...

javaworld

Get a load of that name!

March 14, 2003Q: What is the difference between Class.forName() and ClassLoader.loadClass() ?A: Both methods try to dynamically locate and load a java.lang.Class object corresponding to a given class name. However, their behavior differs regarding which java.lang.ClassLoader they use for class loading and whether or not the resulting Class object is initialized.[ Learn Java from beginning concepts to advanced design patterns in this...

javaworld

Breaking Java exception-handling rules is easy

February 28, 2003 Q: I am calling an external method in my application and want to intercept any exceptions it can possibly throw. Should I catch java.lang.Exception ? A: Handling all runtime exceptions and checked exceptions declared by a given method is not sufficient to build the ultimate defense against external failures. In this Java Q&A installment, I show how any Java method can throw an arbitrary exception—checked or...

javaworld

The thread threat

February 14, 2003 Q: I have a class with a pair of static getter/setter methods that read and write a single static field. Do I need to make them synchronized for thread safety? A: To make the discussion concrete, let's use this class as an example: [ Learn Java from beginning concepts to advanced design patterns in this comprehensive 12-part course! ]public class MaybeSafe { public static int getFoo () { return...

javaworld

Attack of the clones

January 24, 2003Q: What are the advantages and disadvantages of implementing deep cloning via Java serialization and a built-in Object.clone() method from a performance point of view?A: Equipping classes in your application with correct clone() implementation is essential to many defensive programming patterns. Common examples include defensive copying of method parameters, cloning internal fields before returning them in getters,...

javaworld

A splash of text color with your Java

December 20, 2002Q: I want to use colors and fonts in Java console. Is that possible?A: As the simplest option, if your target platform and shell support ANSI escape sequences, you might try sending them to System.out. For instance, ESC [2J is an escape sequence for VT100-compatible terminals and Windows ANSI.SYS drivers that means "erase the entire display":[ Learn Java from beginning concepts to advanced design patterns in this...

javaworld

Profiling CPU usage from within a Java application

November 8, 2002Q: How do you determine CPU usage in Java?A: So, here is the good news and the bad news. The bad news is that programmatically querying for CPU usage is impossible using pure Java. There is simply no API for this. A suggested alternative might use Runtime.exec() to determine the JVM's process ID (PID), call an external, platform-specific command like ps, and parse its output for the PID of interest. But, this approach...