Meanings of Declaring, Instantiating, Initializing and Assigning an Object

Meanings of declaring, instantiating, initializing and assigning an object

Declaring - Declaring a variable means to introduce a new variable to the program. You define its type and its name.

int a; //a is declared

Instantiate - Instantiating a class means to create a new instance of the class. Source.

MyObject x = new MyObject(); //we are making a new instance of the class MyObject

Initialize - To initialize a variable means to assign it an initial value.

int a; //a is declared
int a = 0; //a is declared AND initialized to 0
MyObject x = new MyObject(); //x is declared and initialized with an instance of MyObject

Assigning - Assigning to a variable means to provide the variable with a value.

int a = 0; //we are assigning to a; yes, initializing a variable means you assign it a value, so they do overlap!
a = 1; //we are assigning to a

What is the difference between instantiated and initialized?

Value vis-a-vis Reference Types

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.

Parts of a Variable

There is the variable name and its value. Two parts.

The variable's name is what you declare it to be. The value is what you assign to it.

Variables are Initialized

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.

Objects are Instantiated

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!)

So declare, initialize, and instantiate come together like this

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 :

  1. A reference variable is declared - MyClass myClassyReference
  2. An object is instantiated (...from/of a given class, implied) - new MyClass()
  3. The object is assigned to the variable. =.

Restating the facts:

  1. A reference-type variable is also called simply "a reference". A "value-type variable" is not a reference.

  2. 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.

  3. 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



The Unbearable Lightness of Being

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.

Java: define terms initialization, declaration and assignment

assignment: throwing away the old value of a variable and replacing it with a new one

initialization: it's a special kind of assignment: the first. Before initialization objects have null value and primitive types have default values such as 0 or false. Can be done in conjunction with declaration.

declaration: a declaration states the type of a variable, along with its name. A variable can be declared only once. It is used by the compiler to help programmers avoid mistakes such as assigning string values to integer variables. Before reading or assigning a variable, that variable must have been declared.

What's the point of being able to declare an object but not instantiating it

You might want to assign to the variable a reference to an instance that was created elsewhere, so it makes no sense to automatically create an instance.

In addition, there may be multiple constructors to choose from, and the compiler doesn't know which one to choose.

Further more, the type of the variable may be the type of an abstract class or an interface, for which you can't create an instance. You can only create an instance of concrete sub-classes or classes implementing the interface.

Declaring vs Initializing a variable?

Edit: @ThisClark said something in the comments, and I went to prove him wrong, and upon reading the spec some more I learned something:

Here's an informative except from the specification:

A var statement declares variables that are scoped to the running execution context’s VariableEnvironment. Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created. [...] A variable defined by a VariableDeclaration with an Initializer is assigned the value of its Initializer’s AssignmentExpression when the VariableDeclaration is executed, not when the variable is created.

Based on my reading of this, the following points describe the behavior (and "correct-ish" usage of the terms) you asked about in your question:

  • A variable declaration (e.g., var foo) causes that variable to be created as soon as the "lexical environment" is instantiated. For example, if that variable were defined within a function body, that function is the "lexical environment", and so variable creation coincides with the instantiation of the function itself.

  • Obviously, variable declarations may or may not be created with an initializer (i.e., the right-hand expression that resolves to the variable's initial value). That's a pretty non-specification usage of the term "initial value", though... let's dig into that a bit more:

  • Technically, per the specification notes here, all variables are initialzed with the value undefined:

    variables are created [...] and are initialized to undefined

    Emphasis on "technically" there, as this is largely academic if an initializer was also provided.

  • Based on what we've already covered, it should be understood that the statement var foo; could exist anywhere within a function body, and moving it anywhere else within the same function would have no effect on the runtime semantics of the function itself (regardless of if/where any actual assignments or other references to foo take place). If that's still confusing, re-read the previous points.

  • The last bit of behavior is the most intuitive part, and that's the assignment of the initializer. This assignment takes place when that line of code is actually executed (which, again, isn't the same point in time when the variable technically got created).

So, to quickly recap:

  • All variable declarations (that use var) are always initialized with undefined upon the initialization of their lexical environment.
  • This initialization probably doesn't count as an assignment, in the technical sense (ha, @ThisClark, you were wrong!!). :)
  • Assignments are the easy part, as they behave the way (and at the point in time) that you expect.

Hope that helps (and that I didn't terribly misinterpret the spec!).

Instantiating objects

Because thats the syntax to make it a new instance.

Declaring an object before initializing it in c++

You can't do this directly in C++ since the object is constructed when you define it with the default constructor.

You could, however, run a parameterized constructor to begin with:

Animal a(getAppropriateString());

Or you could actually use something like the ?: operator to determine the correct string.
(Update: @Greg gave the syntax for this. See that answer)



Related Topics



Leave a reply



Submit