As a regular visitor to Japan for $work for many years, my impression is the reason for mask compliance during COVID is because wearing masks during cold/flu season BEFORE COVID was normal.
My understanding is that part of it is to do with the fact that Japan had "been there, done that, got the postcard" when they dealt with SARS. So COVID was "just another" SARS and the Japanese knew what they had to do.
Japan's handling of COVID was, from a western perspective, exemplary. Japan is a country with 2x the population of the UK. Japan is a country that lives and works in MUCH higher density than the UK. And yet it only had something like 1/10th of the COVID cases/deaths the UK did. Given the immense population density, COVID should have spread like wildfire in Japan, but it did not.
The other part is the Japanese cultural thing where, by and large, you understand you are part of a larger picture and coughing/spluttering/sneezing all over your fellow countrymen is very much not cool. So wearing a mask is simply understood to be the right thing to do. On this subject, see also the famous images of Japanese fans clearing up rubbish from football stadiums during world cups ... I didn't see many drunken western fans doing that.
To be clear, I am not insinuating Japan is perfect, far from it. But there is a lot the West could and should learn from Japan ... if it ever wanted to. :)