I\'ve been hearing these two words used in Microsoft tutorials for VB.NET. What is the difference between these two words when used in reference to variables?
Instantiation refers to the allocation of memory to create an instance of a class whereas initialization refers to naming that instance by assigning the variable name to that instance.
Eg: SqlConnection conn = new SqlConnection();
Here new
is a keyword which allocates memory for an instance and conn
is a variable name assigned for that instance.
To initialize something is to set it to its initial value. To instantiate something is to create an instance of it.
Often this is the more or less same thing. This:
SqlConnection conn = new SqlConnection();
instantiates a SqlConnection
object, and initializes the conn
variable by setting it to the that instance.
Since an object's constructor also sets the object's properties to their default values, it's often correct to say that instantiating an object initializes it. (Misleading, if the object exposes a method that you have to explictly call to initialize it after it's instantiated, as is sometimes the case.)
We can see it this way. For a line of code below:
var p = new Person();
The above line can be read as following two ways:
The subject of reference or context matters. When talking in terms of variable, we use the word initialize. When talking in terms of class/type, we use the word instantiate.
See the Java docs: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
"Point originOne = new Point(23, 94);
Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.
Instantiation: The new keyword is a Java operator that creates the object.
Initialization: The new operator is followed by a call to a constructor, which initializes the new object."
Variables in C# are in 1 of 2 groups. Value types or Reference types. Types like int
and DateTime
are value types. In contrast, any class you create is a reference type. C# strings are also a reference type. Most things in the .NET framework are reference types.
There is the variable name and it's value. Two parts.
The variable's name is what you declare it to be. The value is what you assign to it.
All variables are always given an initial value at the point the variable is declared. Thus all variables are initialized.
For value types, like int
the compiler will give them a valid value if you do not do so explicitly. int
's initialize to zero by default, DateTime
's initialize to DateTime.MinValue
by default.
Reference type variables initialize to the object you give it. The compiler will not assign an object (i.e. a valid value) if you don't. In this case the value is null
- nothing. So we say that the reference is initialized to null.
Humans are born. Objects are instantiated. A baby is an instance of a Human, an object is an instance of some Class.
The act of creating an instance of a Class is called instantiation (Ta-Da!)
MyClass myClassyReference = new MyClass();
In the above, it is wrong to say "... creating an instance of an object..."
edit - inspired by comments discussion
Three distinct things are going on (above) using distinct terminology and that terminology is not interchangeable :
MyClass myClassyReference
new MyClass()
=
.Restating the facts:
A reference-type variable is also called simply "a reference". A "value-type variable" is not a reference.
This: "objectA is an instance of an object" is profoundly wrong. If objectA was "an instance of objectB" then it must be that objectA begins life with objectB's type - whatever that is - and current state - whatever that is. What about creating objects D, E, and F as objectB changes? Nay, nay! It is the conceptual and technical case the "objectA is an instance of a Class". "Instantiation" and "instance of" have precise meaning - an object gets its type, definitions, and values from a Class.
MyClass myClassyReference = null
Generally we don't say "the variable is assigned to null" and we never say "the variable is referencing null", No. instead we say "the variable is null"; or "the variable is not referencing anything", or "the reference is null"
Practical Application:
I jab my finger at your code and say "this instance has an invalid property. Maybe that's why the loop fails. You gotta validate parameters during instantiation." (i.e. constructor arguments).
I see this in your code ,
MyClass myClassyReference;
myClassyReference.DoSomething();
"You declared the variable but never assigned it. it's null so it's not referencing anything. That's why the method call throws an exception."
end edit
A reference type variable's name and value exists independently. And I do mean independent.
An instantiated object may or may not have a reference to it.
An instantiated object may have many references to it.
A declared reference may or may not be pointing to an object.
*Instantiation means to create an instance for a class or object.Initialization means to *initiate the same object or class for any purpose.**