new keyword

Syntax

>>-new-ClassType-(-+--------------+-)-;-><
                   '-ArgumentList-'

                     v----------------' v-----'
>>-new-PrimitiveType-+-[-Expression-]-+-+-----+-;-><
                                        '-[-]-'

Description The new  operator is used to create a new instance of a class or array.

An array creation expression creates an object that is a new array whose elements are of the type specified by the primitive type or the reference type.  The reference type can be any reference type, including an abstract class type or an interface type.

In a class instance creation expression, the class type must name a class that is not abstract.  This class type is the type of the creation expression.  The arguments in the argument list, if any, are used to select a constructor declared in the body of the named class type, using the same matching rules as for method invocations.   As in method invocations, a compilation method matching error results if there is no unique constructor that is both applicable to the provided arguments and the most specific of all the applicable constructors.

Examples
In our first example, we create two arrays; the first is a primitive type array and the second is a reference type array.  The first array is completely defined, as both dimensions have been specified, and all the elements are initialized to zero.   The second array is not completely defined.  The third dimension of the array has been initialized to null references.  The elements in the third dimension will have to be explicitly created, as shown in the last line in the example.

float[][] matrix = new float[3][3];

String[][][] stringMatrix = new String[2][3][];
stringMatrix[1][1][1] = new String("The first String");

The second example demonstrates how to create an instance of class that has a default constructor and a constructor that takes one argument, an int value. 

public class aSimpleConstructorClass {
    int aSimpleValue;

    public aSimpleConstructorClass( int aValue ) {
        aSimpleValue = aValue;
    }
}

aSimpleConstructorClass aSCC1 = new aSimpleConstructorClass();
aSimpeConstructorClass aSCC2 = new aSimpleConstructorClass( 10 );

ngrelr.gif (548 bytes)
Syntax diagrams
Java types
Class constructors

Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.