오류, 기능, 문제해결(JAVA)

한 파일에 클래스가 두 개 존재 할 때

seungmin576 2024. 12. 16. 14:34

public 클래스는 하나뿐이며 public 클래스와 자바 파일 이름은 같아야 한다.

package chap6;

class BirthDay {
    int day;
    int month;
    int year;

    public void setYear(int year){
        this.year = year;
    }

    public void printThis(){
        System.out.println(this);
    }
}

public class ThisExample {
    public static void main(String[] args){
        BirthDay bday = new BirthDay();
        bday.setYear(2000);
        System.out.println(bday);
        bday.printThis();
    }
}

클래스가 두 개 이지만 public 클래스는 아래의 ThisExample 하나이다.

public 클래스의 이름과 파일 이름이 다르거나 public이 두 개 거나 하면 오류가 난다.

일부로 public 예약어를 밑의 클래스에서 지우고 위의 클래스에 적어보니 오류가 남. 파일명과 public 클래스의 이름이 달라서 생김