contract Constructor {

string public name;

// I thought constructor will prevent the name to change but after deployed the contract, calling the function resulted in changing the name?????

constructor(string memory _name) {
    name = _name;

function ChangeName(string memory _newName) public {
    name = _newName;


2 Answers 2


When calling this contract, you must enter a name in the contract call.

For example, if the name of the contract is Lottery, then you would call it like so:


The constructor does not prevent the change of the name, it simply applies certain data when calling the contract.

Constructor as it's defined in the docs:

A constructor is an optional function declared with the constructor keyword which is executed upon contract creation, and where you can run contract initialisation code.


The constructor is what will be called at the deployment of your contract. It allows to run functions or set variables, only once during deployment. It can be useful when you want to pass in arguments instead of hard coding.



The constructor doesn't affect the possibility to change the value of a variable after deployment. Only the variable modifiers such as public , private, internal, constant, immutable ... will have an effect on state variables initialization and visibility.

If you want a variable that can never be changed then use constant;

If you want to be able to initialize a variable at deployment and make it immutable after deployment then use the immutable modifier;

constant & immutable


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.