Solidity – Purpose of Declaring Router Address in a Solidity Constructor

routersolidity

Sometimes when people create a new token they declare a constructor:

constructor () public {
        _rOwned[_msgSender()] = _rTotal;
        
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F);
         // Create a uniswap pair for this new token
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());

What is the purpose of this. Does the router address absolutely has to be included in the constructor. Cannot I create the token then choose a dex later ?

Best Answer

You absolutely can initialize it later, most of the programmers do it in this way because is a good practice to initialize your resources at the constructor, but it might not be your case. You can initialize whenever is reasonable based in your business logic