Syntax explanation for "new" keyword for contracts in Solidity

Viewed 189

I am trying to understand the syntax of C c = new C();

I read the new keyword deploys, initializes state variables, runs the constructor, sets nonce to one, and returns address of new instance.

I read that a state variable a has a 0-ary public getter function a() that returns the value of a.

Questions:

  1. What "is" the getter function of a contract?
  2. Why does new act on the getter function?
  3. In C c = new C(); why do we need the first C to define the variable?
1 Answers

What "is" the getter function of a contract?

Why does new act on the getter function?

You can create a pointer to an already deployed contract by omitting the new keyword. Mind that the address is not a constructor param - it's an address of the external contract.

// points to a contract already deployed on address 0x123
C c = C(address(0x123));

In C c = new C(); why do we need the first C to define the variable?

Solidity is a strictly typed language. So the first C defines a variable type C.

Related