Variable stiffness end-of-arm actuators can add dynamic manipulation capabilities to stiff manipulators and simultaneously enhance safety. The presence of an elastic element in these actuators can be used for absorbing impact energy; or storing energy and utilizing it for performing explosive tasks. The major challenge with variable stiffness actuators is to control their position and stiffness simultaneously to achieve optimal task performance. In this paper, we present an end-of-arm variable stiffness mechanism (VSM) for performing dynamic tasks. We formulate the task as an optimal control problem and numerically solve for the task-specific stiffness profile. We demonstrate the usability of the optimization problem in exploiting the dynamics of the VSM during an explosive hammering task and demonstrate that the time-varying stiffness profile can store energy and leads to improved task performance. As a result, the hammer attains twice as much velocity with variable stiffness compared to fixed stiffness. The hammering performance is further improved by optimizing task completion time and hammer velocity. Moreover, we demonstrate that the VSM stiffness plays a crucial role in minimizing the impact forces transferred to the robot. This paper presents the optimal trajectory and stiffness profile achieved through numerical optimization and then evaluates the proposed mechanism using experiments.