The Java language requires you to write code in classes and provides no other mechanism to do so . Thus you need a class because Java says so. This, by the way, is virtually the case for all purely object-oriented languages. Because this is a reserved keyword in Java.
Read moreIs all Java code in a class?
Java Classes/Objects Java is an object-oriented programming language. Everything in Java is associated with classes and objects , along with its attributes and methods.
Read moreDoes Java main have to be in class?
The main method is static , so it doesn’t matter which class holds it.
Read more