Solidity – Can a Function with the Same Name as the Contract Be Declared as Both a Function and Constructor?

constructorsolidity

A function with the same name as the contract is normally a constructor and it’s bytecode isn’t included in the resulting contract.

But is there a way to declare a function with the same name as the declared contract name so it’s both a constructor and a function inside the resulting contract with a signature in the function selector bytecode ?

If yes, was it introduced recently or is it part of the language nearly since the beginning ?

Best Answer

In Solidity version 0.4.24 declaring a function with the contracts name as as constructor has been depreciated in favor for constructor()

If you are using an older version of Solidity than 0.4.24, you declare the function as function contractName().

Similar to this answer constructors are called once, and only once. They are initiated once a contract has been published, and cannot be called again.

When attempting to use constructor() and function contractName() in the same contract, it detects two constructors resulting in DeclarationError: More than one constructor defined. Meaning that the EVM can detect both as acting constructors and will not compile. Implying function contractname() even when declared as public cannot be referred to as a function.

Related Topic