Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Thay thế 'public' bằng 'private' trong 'main' trong Java


Khi dùng public trong main:

Ví dụ

public class Demo{    public static void main(String args[]){       System.out.println("This is a sample only");    } }

Kết quả

This is a sample only

Lớp Demo chứa hàm main là public. Có hàm in, biên dịch, thực thi thành công in thông báo trên console.

Khi public thay bằng private:

Ví dụ

public class Demo{    private static void main(String args[]){       System.out.println("This is a sample only");    } }

Kết quả

Error: Main method not found in class Demo, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application

Lớp Demo chứa hàm main là private thay vì public. Có hàm in, không biên dịch thành công, lỗi main method not found, vì không truy cập được do private.