Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Solidity, can the constructor function have the same name as the contract?

According to this article, the constructor - or initialization - function has the same name as the contract. However, the remix compiler returns an error when I have a function with the same name, saying "defining constructors as functions with the same name as the contract is deprecated. Use "constructor(...) { ... }" instead."

So, my question is, is using the same name in this situation something that should simply be avoided? (Perhaps it is an out of date practice?)

like image 843
CreatedAMadman Avatar asked Oct 19 '25 14:10

CreatedAMadman


1 Answers

Since Solidity v0.4.23, constructors are now specified using the constructor keyword:

    constructor () {}

Just to compare with the old ways, say we have a Smart Contract called Employee, in the past, you'd specify the contract with its constructor like this:

    contract Employee {
      // constructor
      function Employee() public {
        // ...
      }
    }

But the acceptable way to specify the Employee Smart Contract now is:

    contract Employee {
      // constructor
      constructor() public {
        // ...
      }
    }

Using the name of a contract as its constructor is now deprecated.

like image 66
Daniel Okwufulueze Avatar answered Oct 22 '25 05:10

Daniel Okwufulueze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!