To access a contract we need the address of the deployed contract right?
Right. However, in this example, the OwnedToken
contract is actually created by an existing, already-deployed instance of a TokenCreator
contract. See below where the TokenCreator
contract calls new OwnedToken(name)
. When a contract creates another contract, the creator contract is the msg.sender
of the created contract. Since it wants to later be able to call methods on its creator contract - specifically isTokenTransferOK()
- it stores its address in its constructor.
How is the contract available in the first place?
See above: Somebody had already mined TokenCreator
, which has a method that creates an instance of OwnedToken
.
Will the contract be still available if they both are deployed seperately i.e, one after another?
Well, you could have created an OwnedToken
separately rather than making a method in TokenCreator
that could create contracts. But OwnedToken
needs to know the address of TokenCreator
for when it needs to call isTokenTransferOK()
. The way it's currently written that's done by relying on the fact that the relevant TokenCreator
was its msg.sender
. If you create the contract yourself then your address will be msg.sender
, so that wouldn't work. You'd need to write it slightly differently - for example by adding an extra parameter to the constructor allowing you to pass in the address of the relevant TokenCreator
. (You'd probably also rename TokenCreator
to something like TokenController
, since it wouldn't be creating tokens any more...)