What is the difference between “instantiated” and “initialized”?

后端 未结 11 2152
青春惊慌失措
青春惊慌失措 2020-11-30 17:02

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?

相关标签:
11条回答
  • 2020-11-30 17:51

    Instantiation is when you create an instance of a class. That instance is then an object, and you can set its properties, or call methods on it (tell it to do things).

    Initiation is when you set up a set of initial conditions for something. That something might be an object, where you tell it to initiate itself, or just a variable to which you assign a value.

    An object might initialise some other things, or even instantiate other objects as part of its initiation.

    The difference is that instantiation is creation of a thing that can do stuff; initiation is stuff that gets done.

    0 讨论(0)
  • 2020-11-30 17:56

    When you instantiate a class or object, you're creating a new instance of it, or allocating memory to "hold" one. Initializing that object would be the instructions that are performed during instantiation.

    0 讨论(0)
  • 2020-11-30 17:57

    Others have explained the difference, so I wont go into detail. But there are cases where instantiation does not properly initialize an object. When you instantiate an object you also initialize it with some data. The class/type will have the initialization logic, whereas the instantiation logic is typically carried out by thenew keyword (basically memory allocation, reference copying etc). But instantiation need not necessarily result in a valid state for objects which is when we can say the object is uninitialzed. Here's a practical example where an object can be instantiated but not initialized (sorry e.g. in C#).

    class P { string name = "Ralf"; }
    
    WriteLine(new P().name); // "Ralf";
    WriteLine((FormatterServices.GetUninitializedObject(typeof(P)) as P).name); // null
    

    GetUninitializedObject doesn't call the constructor to instantiate object there (but some internal magic).

    One could also argue value types are not instantiated but only initialized as it doesn't need new allocation when you do new.. but that's up to one's definition of instantiation.

    0 讨论(0)
  • 2020-11-30 17:58

    A variable is initialized with a value. An object is instantiated when memory is allocated for it and it's constructor has been run.

    For instance here is a variable:

    Dim obj as Object
    

    This variable has not been initialized. Once I assign a value to the obj variable, the variable will be initialized. Here are examples of initialization:

    obj = 1
    obj = "foo"
    

    Instantiation is a very different thing but is related since instantiation is usually followed by initialization:

    Dim obj As New Object()
    

    In the preceding line of code, the obj variable is initialized with the reference to the new Object that was instantiated. We say that the new Object was instantiated because we have created a new instance of it.

    Now I believe that VB.NET makes this a lot more confusing than C# because it is not clear that an assignment is taking place in the code above. In C# it is much clearer that there is both an instantiation of an instance and an initialization of a variable:

    Object obj = new Object();
    
    0 讨论(0)
  • 2020-11-30 18:00

    Instantiated means that an instance of the object has been created. Initiated means that that same object has done some initialization.

    0 讨论(0)
提交回复
热议问题