22. Practical on Constructors in Remix

Let's take some, a look at some code related to constructors.

Right here we have the contract MyContract.

Inside, let's define some variables first.

I'll define a variable called uint, which is public call it num1.

I'll define an address which is public called 'owner'.

If you remember carefully from the modifiers example, we have done something similar where we declared an address called 'owner' and used it for a modifier which let this specific address access certain functions and no other address would be able to access those functions.

So now let's declare a constructor though.

If you remember in that example, we had simply done something like this.

We had pasted an address from here.

By simply copying it and pasting it.

However, this is not exactly the best way to do this.

We can do actually do this using constructors.

The constructor is of course a special type of function which will be executed when the contract is

created and after that it will be never executed again.

That is the special thing about it.

It essentially acts like an initializer of your contract.

You want to initialize some of the variables.

You want to set them to be specific values.

For example, let's say you are opening a babysitting service.

You have gotten registrations for, let's say, 6 kids.

So in your database, you want to set the number of kids to be 6.

So when you start the database, you initialize it to be 6.

And that's essentially how you can use a constructor as well.

So imagine 'num1' to be the number of kids you want in your babysitting service.

So what you do is simply you have the constructor public, the curly brackets.

And what you do is set num1 to be equal to 6.

And the next thing you do is set owner to be equal to msg.sender.

Why did we use msg.sender here again?

You see, the constructor is also a function.

So when you are calling for contract creation, you're actually calling this constructor as well.

And you as an EOA, when you are creating a contract, your msg.sender of course, goes in.

Which is of course, your address.

So what will happen is that when you run this constructor, the creator of this contract will be set as the owner of the contract, which of course means that the owner variable will be set to message.sender.

Okay, so let's run this contract and see what happens.

You can see here, I have set my account at this 5B3 address.

I'll click on deploy and right here I have my deployed contract.

So if you remember from examples that we did before, we had not used any type of constructor and we had not even set any value for num1.

So at that time we got the default value to be 0.

But this time we have a constructor.

So what is expected that in the constructor this code was executed and now num1 will hold the value 6.

Meanwhile, the owner variable will hold the value msg.sender, which is of course this value 0x5B3, this specific value.

Let's just put in comments right here.

This will be the value.

And that is because we use this address to deploy this contract or create this contract essentially setting msg.sender to be this and setting owner to be this as well.

So let's first take a look at num1.

We get the getter function for num1.

We can click on it.

As you can see, it is number 6 that num1 holds.

Next, let's take a look at the owner.

You can see right here.

Compare this with this, it is the same.

That is basically how constructors are used.

This is, of course, a simple example showing.

Simply setting two different state variables we can do, but we can do much more with constructors.

That is about it for this video.

Complete and Continue  

Become a Member and Get Unlimited Access to 30,000+ Top Cyber Security Courses And Labs.