Counters before or after constructor

Why is the counter initialized before the constructor? Does it matter? Can I put other variables above the constructor? Could I put the counter variables below the constructor?


// SPDX-License-Identifier: MIT pragma solidity ^0.8.4; 
import "[@openzeppelin/contracts/token/ERC721/ERC721.sol]("; 
import "[@openzeppelin/contracts/access/Ownable.sol]("; 
import "[@openzeppelin/contracts/utils/Counters.sol]("; 
contract MyToken is ERC721, Ownable { using Counters for Counters.Counter; 

Counters.Counter private _tokenIdCounter; 

constructor() ERC721("MyToken", "MTK") {} 

function safeMint(address to) public onlyOwner { 
uint256 tokenId = _tokenIdCounter.current(); 
_safeMint(to, tokenId); } }


Please use triple backticks to surround your code in order to get proper formatting.

code here

You can put the variable before or after the constructor. The convention is to put it before.

1 Like

Thanks for the answer and thanks for the tip about the code. That's way better.