As we know from official TestNG documentation:
@BeforeClass: The annotated method will be run before the first test method in the current class is invok
What is missing in the above answers is the usage difference between @BeforeClass and @BeforeTest annotations.
As it is clear that the method (most commonly the setup method) annotated with @BeforeClass will execute only once before all the testcases written in the class. And the method annotated with '@BeforeTest' will execute before every testcase regardless of their count/sequence/underline logic.
Therefore, @BeforeClass is used when our method has long calls with long execution time and the output of these calls will be unchanged by any of the testcases. For example, getting an API response in the setup method, which will be used by all the tests.
Whereas, @BeforeTest@ is used when we need to do some cleanup stuff, and every test needs some fresh resource to start with. For example, one newly created order etc.