Yeah, it's Hollywood. So they wont follow the rules or expect realism, and would likely have hand-waved everything. But I was curious if they attempted an explanation.
But with the assumption that the movies are contemporary to their release dates (so Top Gun is set in 1986 and Maverick 2022):
- Maverick and Iceman are both Lieutenants (O3) in 1986 in Top Gun, meaning they entered the service between 1976 and 1982. We'll use 1982 because it gives us the most time to work with, though 1980 or sooner would make more sense given level of experience in the movie..
- In Maverick, Maverick is a Captain (O6) in 2022. Without a break in service, that's 40 years of service -- but O6 are statutorily limited to 30 years of service (outside of one very specific exception I'm aware of for service academy professors). So he's about 10 years past mandatory retirement for his rank -- and admirals don't get to waive the law!
- Age-wise mandatory retirement for non-flag officers is 62, so assuming a normal commissioning age of 22 in 1982, Maverick is also sitting at mandatory retirement age so even if he had some significantly-long break in service he should most likely be out.
- Flag officers can serve past 30 years. As a four-star (O10) Iceman could conceivably serve 40 years (the current Chairman of the Joint Chiefs is sitting on 42 years of service), but will bump up against mandatory retirement age at 64 (though this can be deferred to 68 under special circumstances).