DuraSoft  TechLet 07, 2002

In this issue
Welcome to the July 2002 TechLet. If you received this through some one and would like to subscribe for future articles, please send an email to techlet@durasoftcorp.com with a subject subscribe.

In this issue, we have a detailed article on the issues with copying and cloning objects in Java, C# and C++.

In depth
Why Copying an Object is a terrible thing to do? Copying an object by specifying new followed by the class name often leads to code that is not extensible. Using clone, the application of prototype pattern, is a better way to achieve this. However, using clone as it is provided in Java (and C#) can be problematic as well. In this article we address the issues with copying objects, and show how to write code to avoid these problems. A standard practice to be followed in writing classes is also provided here. The complete article can be found at http://www.durasoftcorp.com/download
Quiz corner
A class keeps track of the number of objects created. Partial code is shown below:
public class A
{
	private static int objCount = 0;
	public A()
	{
		synchronized(getClass()) { objCount = objCount + 1; }
	}
	...
}

The constructor is performing a thread safe increment. Is this increment well protected? What could go wrong? The answer will be in the next issue!


Quiz from the past issue

This questions was posted by one of the attendees of our classes. We took that question and modified it slightly.
Write a Java program that meets the following criteria:

  • It should be a command line application that you will run by typing java MyApp
  • It should have no main static method
  • It should print Hello there! on the window and absolutely nothing else

    Answer!

    public class MyApp
    {
    	static
    	{
    		System.out.println("Hello there!");
    		System.exit(0);
    	}
    }
    
    The static initializer is called when the class is loaded. The class is loaded, when you execute java MyApp. System.exit() will terminate the virtual machine!
  • Message from us
    If you like any question to be addressed or topic to be discussed, please send us an email to techlet@durasoftcorp.com. At the end of the year, one person will be randomly selected from the list of respondents for a special prize. We would like to hear from you.

    Please send suggestions, corrections, comments to techlet@durasoftcorp.com.

    Thus spake...
    Example isn't another way to teach, it is the only way to teach - Albert Einstein.