possession after a shot

saw a game on TV today, and made me wonder... i know possession goes to whoever is closest when it goes out. does the player have to be inbounds? shot riccochets high, attackman runs to the endline and actually beats the ball there. Does he get it or the player closest that is inbounds?