Only a Captain within the Military holds solely a rank, though
his appointment might be in any area of the Service he is in,
and is titled by convention whether in uniform or not. Also he
retains the title after active service (usually with a "Retired"
suffix).
Also in the military a captain may be a title and not a substantive rank. The CO (Captain) of a RN ship or submarine may be a LtCdr, Cdr or historically a Lt. Captain of the Gunhouse was a Petty Officer. I myself was Captain of the Heads more than once as a young AB.