Since there’s contradictory statements by various characters as to Starfleet being a military and not being a military, perhaps an in universe explanation is that Starfleet operates similarly to the US Public Health Service and NOAA Commissioned Corps.
Each entity is considered 1 of the 8 US Uniformed Services with the armed forces constituting the other 6. The President can by order militarize the the PHS and NOAA Commissioned Corps.
33 U.S.C. § 3061 states:
The President may, whenever in the judgment of the President a sufficient national emergency exists, transfer to the service and jurisdiction of a military department such vessels, equipment, stations, and officers of the Administration as the President considers to be in the best interest of the country. An officer of the Administration transferred under this section, shall, while under the jurisdiction of a military department, have proper military status and shall be subject to the laws, regulations, and orders for the government of the Army, Navy, or Air Force, as the case may be, insofar as the same may be applicable to persons whose retention permanently in the military service of the United States is not contemplated by law.
42 U.S.C. § 217 states:
In time of war, or of emergency involving the national defense proclaimed by the President, he may by Executive order declare the commissioned corps of the Service to be a military service. Upon such declaration, and during the period of such war or such emergency or such part thereof as the President shall prescribe, the commissioned corps (a) shall constitute a branch of the land and naval forces of the United States, (b) shall, to the extent prescribed by regulations of the President, be subject to the
Uniform Code of Military Justice,
10 U.S.C. § 801 et seq., and (c) shall continue to operate as part of the Service except to the extent that the President may direct as Commander in Chief.
The Federation President may be able to militarize Starfleet in a legal sense when war or other emergencies exist because there is no other military entity. This may be done so that Starfleet vessels can preemptively attack enemy vessels, raid installations, take prisoners of war, and be recognized as prisoners of war when captured, etc. This would also explain in universe how some Starfleet officers think that they are not the military because in it’s normal state, Starfleet is not considered as such but can be under certain circumstances.