Instantiating Reference Types Using the new Keyword

To create an object of a particular type, you need to instantiate the type.  Value types are instantiated by assigning them a value.  Reference types are instantiated using the new keyword.  Using new allows us to create a new instance of the type.
When new is used to instantiate a type, the type’s constructor is called to perform the initialization.  The type may have a default constructor that takes no parameters, or it may have a constructor that takes one or more parameter values.  It may also support multiple constructors.
Variables declared as instances of reference types will hold the value null until they are instantiated.

Person p1;     // Not instantiated, value is null
 
// p2 points to new instance of the Person class
//   Default constructor, takes no parameters
Person p2 = new Person();
 
// Construct another Person object using a
//   different constructor, which takes Name and Age
Person p3 = new Person("Sean", 46);
Previous
Next Post »