: code tutorial (v3)

Lesson 2: Constructing Proxy Objects

Introduction

Let's look at a few lines of Java code that deal with type java.util.Hashtable.

// creates a hashtable using the default (unparametrized) constructor 
Hashtable       ht1 = new Hashtable();

// creates a hashtable with initial space for 15 entries
Hashtable       ht2 = new Hashtable( 15 );

// does not create an object; initializes object reference to null
Hashtable       ht3;

If you have a solid Java background, it is probably unnecessary to point out that the new keyword is used to signal that you are creating a new object instance by invoking the immediately following constructor. In Java, the two steps of object creation:

  1. memory allocation (new)
  2. object initialization (constructor)

are inseparably intertwined and you cannot do one without the other.

In .NET things are totally analogous. The following C# snippet implemented in terms of proxy types has exactly the same functionality as the above Java snippet.

using Java.Util;

// creates a hashtable using the default (unparametrized) constructor 
Hashtable       ht1 = new Hashtable();

// creates a hashtable with initial space for 15 entries
Hashtable       ht2 = new Hashtable( 15 );

// does not create an object; initializes object reference to null
Hashtable       ht3;

Take-Away Points

Constructors Translate Exactly as Expected.

If you have a Java program that uses constructors, the corresponding proxy program will have exactly the same constructor invocations.