[Ethereum] Should the constructor function be public


In all the tutorials I'm watching nobody specifies public, but remix presents warnings that function visibility has not been specified, and public is assumed.

Since I don't like warnings if possible, I am specifying public where necessary, but I wonder if the constructor function itself should be public or private. It seems to me that it should be a private one, as it's called automatically by the contract itself on creation, or is my reasoning faulty?

Best Answer

When you are trying to compile contract with private constructor, for example this one:

pragma solidity ^0.4.15;

contract PrivateConstructor {
    string public title;

    function PrivateConstructor() private {
        title = "Private Constructor";


you receive this error:

TypeError: Constructor must be public or internal.

So looks like contract counstructor could be only public or internal.

More details here Visibility and Getters

Related Topic