In theory, a constructor is just a function like any other function, but only gets called once at construction time. Because the EVM doesn't know the notion of a constructor, in theory, a constructor can be called multiple times in EVM Opcode terms. This could be a security leak as developers do not think that constructors are called multiple times. For example, the contract owner address could be overwritten then.

Is this possibility prevented and if so how?

2 Answers 2


The constructor function is actually not just a regular function, and actually is called exactly once and cannot be called again.

Not only does the EVM not know what a constructor is, it also doesn't know what a function is. The way functions are implemented in Ethereum is that the compiler includes a check at the beginning of the code which compares the first four bytes of the transaction data with the function signatures, computed as bytes4(sha3("myFunction(uint256, bytes, address, ...)")).

The constructor is not included in the list of function signitures that it checks, and the constructor code is not even included in the contract.

Contracts are created by sending a transaction with no to field. The data in the transaction is interpreted as EVM code. This code (which includes the constructor function) is run, and its return value (which does not include the constructor) is used as the new code in the contract.

Thus there is no security hole here.

  • Does the answer below is true ? May 28, 2018 at 19:47
  • "with no to field" I guess this was changed in Homestead? When I presign a transaction with ethereumjs-tx, I have to put the contract address in the to field. Or is it that we're doing just that to make the code more intelligible for developers, even if there's no destination per se? Jul 8, 2019 at 18:37
  • A contract creation transaction has an empty "to" field. In ethereumjs it'll be something like to='' Jul 8, 2019 at 19:07

Although your answer is correct it is not always the case.

function constructor() public { owner = msg.sender; }

Problem here is the keyword function before constructor. It is used as a constructor but can be called because it is a public function as well.

  • @VigneshKarthikeyan In that case, does removing the public keyword still make the function both valid as a contructor and a public function ? May 28, 2018 at 19:47
  • No, this answer is definitely incorrect. When the keyword function is used, it's a function and will not be used as constructor. Try this. x will be 0 after contract creation. The compiler even has an explicit warning for this.
    – mafrasi2
    May 28, 2018 at 21:27
  • @mafrasi2 : did you try with the compiler version which include the constructor keyword ? May 28, 2018 at 21:56
  • @user2284570 yes
    – mafrasi2
    May 28, 2018 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.