Letters to the Editor

What's the difference between Jxta and Jini? Find out in this month's letters.

1 2 Page 2
Page 2 of 2

You describe Model-View-Controller (MVC) as a design pattern. I believe MVC is an architecture that includes some design patterns. Kindly, correct me if I'm wrong.

Ahmed Aboul-Enein

Ahmed, In the past some literature has called MVC a pattern; others call it an architecture. But you are right: the current consensus seems to be to call it an architecture. The J2EE (Java 2 Platform, Enterprise Edition) Patterns Catalog and the Gang of Four also seem to support that theory. Now I agree as well. Govind Seshadri

Java 101

'Class and object initialization'

Jeff Friesen

Object initializers are useful

Jeff,

You state:

Contrary to my feelings about the class block initializer, I do doubt the usefulness of an object block initializer. It seems that everything you can do in an object block initializer, you could also do with a constructor. That does not mean object block initializers are not useful -- only that I would like to see a good use for them.

I can present a use for object initializers from my own experience: A class has multiple constructors. Each constructor has a common segment of code. I declare a private method private void commonCode() and invoke this method in every constructor. It could be replaced with an object initializer.

Ramesh

Ramesh, To avoid explicit duplication in source code, the object block initializer is the perfect place for such code. I put together the following source code:

 
// Test.java
 
class Test
{
   {
      commonSetup ();
   }
 
   Test ()
   {
      this (0);
   }
 
   Test (double d)
   {
      System.out.println ("Test() d = " + d);
   }
 
   Test (int x)
   {
      System.out.println ("Test(int) x = " + x);
   }
 
   void commonSetup ()
   {
      System.out.println ("Common setup");
      System.out.println ("More common setup");
   }
 
   public static void main (String [] args)
   {
      Test t1 = new Test ();
      Test t2 = new Test (5);
      Test t3 = new Test (1.0);
   }
}

When run,

Test

produces the following output:

 
Common setup
More common setup
Test(int) x = 0
Common setup
More common setup
Test(int) x = 5
Common setup
More common setup
Test() d = 1.0

Apart from

Test()

, each constructor explicitly calls

commonSetup()

-- because the compiler places object block initializer instructions into equivalent

<init>

methods. The

Test()

constructor indirectly calls

commonSetup()

-- at the

<init>

method level -- through the

Test(int x)

constructor. There is one caution that comes to mind with this approach: Suppose I introduce another

Test

constructor that should not call any other

Test

constructor or

commonSetup()

-- at the

<init>

method level (example:

Test (boolean b) { }

). Due to the

Test

program's current architecture, there does not appear to be a way to prevent such a constructor from calling

commonSetup()

. Regardless of the above caution, your suggestion has much merit. Jeff Friesen

1 2 Page 2
Page 2 of 2