Java Specialists' Java Training Europehome of the java specialists' newsletter

The Java Specialists' Newsletter
Issue 0872004-04-09 Category: Language Java version:

GitHub Subscribe Free RSS Feed


by Dr. Heinz M. Kabutz

Welcome to the 87th edition of The Java(tm) Specialists' Newsletter. We have a new translation into Greek, thanks to Panos Konstantinidis. The English have an expression: "It's Greek to me.", which means that they could not understand what was being said. The Greeks have a similar saying: "It's Chinese to me." When I was in Greece, my wife's cousin explained it like this: "Chinese is the most difficult language, because of these two sayings." When I was in China last year, I asked whether they had a similar expression. Imagine if the Chinese said: "That's English to me!" then we would have a loop in the logic.

This year we decided to not have our traditional "April Fools" Java newsletter. However, I thought that Sun Microsystems and Microsoft had gotten the date wrong, when they anounced on the 2nd of April that MS had opened up their petty cash box and found some spare $2,000,000,000 that they thought should go to a good cause. Time will tell what the implications are for us Java developers...

I would like to thank Matthew Schmidt and Rick Ross from Java Lobby for the nice things they wrote about our newsletter: "In my day-to-day quest for deeper Java knowledge, I often find myself consulting the newsletter resources of Dr. Heinz Kabutz of Heinz specializes in Java consulting, helping you get the most out of Java. Based in South Africa, he brings Java to places many of us have never seen or heard of before. Every two weeks or so he sends out a newsletter chocked full of Java code. One of his more recent ones รข€“ an article on Axis - has been nearly indispensable to me during my both my design project work and recent Javalobby work. If you love reading brain-teasing newsletters filled with code (and I know I do), then you'll love his newsletter."

NEW: Please see our new "Extreme Java" course, combining concurrency, a little bit of performance and Java 8. Extreme Java - Concurrency & Performance for Java 8.


A few weeks ago, I was chatting to my friend Niko Brummer, who is an expert in speaker verification. Niko loves watersports and frequently applies his theoretical knowledge to his sports, much to my amusement. Niko is always available for a chat about his latest discoveries in Java (and in the ocean).

When we were chatting, Niko mentioned a class called "Reflection" that I had not heard of before. This class comes with Sun's JVM and sits in the sun.reflect.* package. The most useful method in this class is: Class getCallerClass(int i). This method tells you which classes are in our call stack.

Let's look at an example of some classes for making soup. We want the Potato to have a reference to the Soup in which it swims, and we want the Soup to know which Potatoes it contains. We have a one-to-many relationship and we want this to be maintained correctly. Please remember that this solution is just one of many, and is simply an example of how you could use this class.

import java.util.*;
public class Soup {
  private final List potatos = new ArrayList();

  public void add(Potato p) {

  public String toString() {
    return "Soup {potatos=" + potatos + "}";

import sun.reflect.Reflection;

public class Potato {
  private final int id;
  private Soup soup;

  public Potato(int id) { = id;

  public void setSoup(Soup soup) {
    this.soup = soup;
    if (Reflection.getCallerClass(2) != Soup.class) {

  public Soup getSoup() {
    return soup;

  public String toString() {
    return "Potato " + id;

The interesting method is Potato.setSoup(). It checks whether the calling class is Soup. The method getCallerClass() takes the stack depth as a parameter, in this case it would be 2.

We can try this out by making a soup, adding some potatoes, and then associating the soup with a potato that did not have an association.

public class SoupTest {
  public static void main(String[] args) {
    Soup soup = new Soup();
    soup.add(new Potato(1));
    soup.add(new Potato(2));
    soup.add(new Potato(3));
    Potato p4 = new Potato(4);
    p4.setSoup(soup); // redundant code
    Potato p5 = new Potato(5);
    System.out.println("soup = " + soup);

When I run this with java -showversion SoupTest:

java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)

soup = Soup {potatos=[Potato 1, Potato 2, Potato 3, Potato 4, Potato 4, Potato 5]}

A handy little class supplied in the Sun JDK.

Kind regards


Language Articles Related Java Course

Extreme Java - Concurrency and Performance for Java 8
Extreme Java - Advanced Topics for Java 8
Design Patterns
In-House Courses

© 2010-2016 Heinz Kabutz - All Rights Reserved Sitemap
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. is not connected to Oracle, Inc. and is not sponsored by Oracle, Inc.