The twinkling of a star is due to the atmospheric refraction of starlight. The starlight, on entering the earth's atmosphere, undergoes refraction continuously before it reaches the earth. The atmospheric refraction occurs in a medium of gradually changing refractive index.