Information Technology Reference
In-Depth Information
Creating Variables and Instances
A constructed class type is used just like a regular type in creating references and instances.
For example, the following code shows the creation of two class objects.
￿
The first line shows the creation of an object from a regular, non-generic class. This is a
form that you should be completely familiar with by now.
The second line shows the creation of an object from generic class SomeClass , instanti-
ated with types short and int . The form is exactly analogous to the line above it, with the
constructed class forms in place of a regular class name.
￿
MyNonGenClass MyNGC = new MyNonGenClass ();
SomeClass<short, int> MySc = new SomeClass<short, int> ();
Constructed class Constructed class
As with non-generic classes, the reference and the instance can be created separately, as
shown in Figure 19-6. The figure also shows that what is going on in memory is the same as for
a non-generic class.
￿
The first line below the generic class declaration allocates a reference in the stack for
variable MyClass . Its value is null .
￿
The second line allocates an instance in the heap, and assigns its reference to the
variable.
Figure 19-6. Using a constructed type to create a reference and an instance
Search WWH ::




Custom Search