The number of independent variables in the Lagrangian and Hamiltonian methods in Classical Mechanics

1L) The (generalized) position $q$ and (generalized) velocity $v$ are independent variables of the Lagrangian $L(q,v,t)$.

1H) The position $q$ and momentum $p$ are independent variables of the Hamiltonian $H(q,p,t)$.

2L) The position path $q:[t_i,t_f] \to \mathbb{R}$ and velocity path $\dot{q}:[t_i,t_f] \to \mathbb{R}$ are not independent in the Lagrangian action $$S_L[q]~=~ \int_{t_i}^{t_f}\!dt \ L(q ,\dot{q},t).$$ See also this question.

2H) The position path $q:[t_i,t_f] \to \mathbb{R}$ and momentum path $p:[t_i,t_f] \to \mathbb{R}$ are independent in the Hamiltonian action $$S_H[q,p]~=~\int_{t_i}^{t_f}\! dt~(p \dot{q}-H(q,p,t)).$$

3L) Under extremization of the Lagrangian action $S_L[q]$ wrt. the path $q$, the corresponding equation for the extremal path is Lagrange's equation of motion $$\frac{d}{dt}\frac{\partial L(q,\dot{q},t)}{\partial \dot{q}} ~=~ \frac{\partial L(q,\dot{q},t)}{\partial q}.$$

3H) Under extremization of the Hamiltonian action $S_H[q,p]$ wrt. the paths $q$ and $p$, the corresponding equations for the extremal paths are Hamilton's equations of motion $$-\dot{p}~=~\frac{\partial H}{\partial q} \qquad \text{and}\qquad \dot{q}~=~\frac{\partial H}{\partial p} ,$$ respectively.

4L) The equation $p=\frac{\partial L}{\partial v}$ is a definition in the Lagrangian formalism. E.g., for a non-relativistic free point particle, it encodes the relation $p=mv$.

4H) The equation $\dot{q}=\frac{\partial H}{\partial p}$ is an equation of motion in the Hamiltonian formalism. E.g., for a non-relativistic free point particle, it encodes the relation $p=m\dot{q}$.


$q_j$ and $\dot q_j$ are independent. I think it's more straightforward (at first) to think of this in terms of Newton's equations of motion, where the force determines the accelerations of the various particles, than in terms of the more abstract Hamiltonian methods. Because the forces determine the accelerations, not the velocities, both the initial positions and the initial velocities have to be given to determine the trajectories, which is just to say that the $q_j$ and the $\dot q_j$ independently determine the trajectories.

Note that the Lagrangian function is written as a function of both $q_j$ and $\dot q_j$, $L(q_j,\dot q_j)$, which makes sense of the equation for the momenta that you cite, $p_j=\frac{\partial L(q_j,\dot q_j)}{\partial \dot q_j}$.

So, there are the same numbers of independent variables in the Lagrangian and in the Hamiltonian formalisms.