In die klas literally means “in the classroom,” indicating where the joke is told. By die klas would mean “by the classroom” (standing next to it). If you want “to the class” as an indirect object (telling the students), you’d say vir die klas (or just die klas as a direct recipient):
• Hy vertel die klas ’n grappige grap.
• Hy vertel vir die klas ’n grappige grap.