Usually, guys get erections in the morning to prevent them from peeing. That's called "morning wood" and isn't even a part of puberty - it happened to me before puberty, during puberty, and it happens to grown men as well.
What you're getting during class is "spontaneous erections". You get an erection for no reason at all. EVERY SINGLE GUY IN PUBERTY GETS THIS, AND IT'S FREAKIN ANNOYING
It's because of your hormones. There's really no way to stop it, but once you get an erection, just try to relax don't concentrate on it. It'll go away eventually.