Below is my code how to write this code without object manager.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
//get current product
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$productId = $product->getId();