Có, chúng ta có thể khai báo phương thức main là private trong Java.
Nó biên dịch thành công không lỗi nhưng tại runtime, nó báo lỗi phương thức main không phải public.
Ví dụ:
class PrivateMainMethod {
private static void main(String args[]) {
System.out.println("Welcome to Tutorials Point");
}
}
Mã trên chạy thành công lúc compile nhưng ném lỗi lúc runtime.
Kết quả:
Error: Main method not found in class PrivateMainMethod, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application