EDDYMENS

Eddymens (List page)

Last updated 2023-09-04 06:15:48

What Is The Difference Between Instantiating And Initializing In Programming?

Table of contents

Definition

Instantiating and initialization are some of those terms that have an almost similar definition that we turn to use them interchangeably. There is, however, a bit of a difference in what they stand for.

In programming, there are different concepts of code templates you can create and reuse when you need them. These take the form of objects and on a higher level classes. There are many others but these two are very common in modern programming languages.

These structures allow you to capture common data structures and logic or behavior which you can then create copies of and tweak a bit by passing parameters to them.

The process of creating this copy is known as creating an instance of the class or object.

When you pass in the parameters to get the desired behavior, we say you have initialized the class or object.

So in short an instance is still a generic copy of the original template while an initialized copy is a tweaked or differentiated copy of the original object or class.

Examples

Let's look at an actual example involving classes in Javascript

Class declaration

01: 02: class Room { 03: constructor(height, width) { 04: this.height = height; 05: this.width = width; 06: } 07: 08: get area() { 09: return this.height * this.width; 10: } 11: }

In the above code example, we create our "class template" from which we will create other copies. This is known as declaring a class.

Class instantiation

01: roomInstance = new Room; // creating an instance of the declared class 02: // output: Room {height: undefined, width: undefined} 03: 04: roomInstance.width = 2; 05: roomInstance.height = 3; 06: 07: roomInstance.area // output: 6

In the example above we create a "new" instance of the declared class on line 01:. This instance is very similar to the declared class because the height and width properties are similar which is empty.

We go on later to define the width and height on lines 04 and 05 which can be seen as initializing the instance making it different from the original class.

This however might not be seen as a proper initialization process as it feels like it's not the initial thing that was done.

Below we will look at a proper initialization process.

Class initialization

01: roomInstance = new Room(1,2); 02: // output: Room {height: 1, width: 2} 03: 04: console.log(roomInstance.area); // output: 2

In this last example, we see something interesting not only are we creating an instance of the class, we are also initializing it at the same time on line 01. And it's because of this simultaneous process that blurs the difference between instantiation and initialization.

Summary

No matter which word you use most people will understand what you mean since in most cases both events happen at the same time. But from time to time you might need to point out exactly what is happening. Say you have a class that is just a copy of the declared class and so you want to make it clear to another developer that it's a class similar to the original copy.

A good example is when talking about dependency injection [→].

Here is another article you might like 😊 "What are namespaces?"