This isn't highschool. It's not their place to decide what your appearance should be like.
He sounds like he has a personal problem with you by the way he addressed you in front of the class,
or, he may have done it to make an example of you to give a heads up to any other long-haired gentlemen in his class.
Either way, talk to him and try to work it out, and if he won't budge on it, tell him to frack off. (not seriously recommended)
(edit) Follow mal's advice.