16

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?

13

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

  • 2
    When using internal: "This contract does not implement all functions and thus cannot be created." – Nico Mar 4 '18 at 12:15
  • 3
    @Nico this might be a bit late but when using "internal" the contract gets marked as abstract, meaning it can only be used as a base contract and will fail to compile on its own. You will have to inherit the contract and implement all functions in the derived contract. If you do not implement all functions, the derived contract will also be marked abstract - giving you the same issue. – Bracher Mar 26 '18 at 21:48
5

A constructor can be internal or public; however, if the constructor has a 'payable' modifier, then the constructor can only be public.

Regarding internal visibility, it makes it possible for an abstract contract to have a constructor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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