使用netBeans在不同类中实例化类中的对象

时间:2014-06-27 21:34:21

标签: java

我似乎无法从我写入main方法的类中实例化一个对象。我不知道我的代码或netBeans是否存在问题。以下是仅针对该问题发表评论的程序:

package app;
import java.util.Scanner;
public class SetUpSite {
public static void main(String[] args) {
    final int FOUNDED_YEAR = 1977;
    int currentYear;
    int age;
    statementOfPhilosophy();
    Scanner reader = new Scanner(System.in);
    //the very next line is the only line unrecognized
    EventSite oneSite = new EventSite();
    int siteNum;
    System.out.print("Enter the event site number: ");
    siteNum = reader.nextInt();
    oneSite.setSiteNumber(siteNum);
    System.out.print("Enter the Current year of the company: ");
    currentYear = reader.nextInt();
    System.out.println("The age of the Company is "+ calculateAge(FOUNDED_YEAR,        currentYear) + " years");

}

public static void statementOfPhilosophy() {
System.out.println("Event Handlers INC");
}
public static int calculateAge(final int ORIGINAL_YEAR, int currentDate) {
    int years = currentDate - ORIGINAL_YEAR;
    return years;

}

public class EventSite {
private int siteNumber;

public void oneSite() {

}
public int getSiteNumber() {
return siteNumber;
}

public void setSiteNumber(int n) {
    siteNumber = n;
}

}
}

1 个答案:

答案 0 :(得分:1)

EventSite是SetUpSite的公共内部类,这让你烦恼不已。从SetUpSite类中获取EventSite类代码,而不是将其放在它自己的文件中。 Java文件不能包含多个顶级公共类。

你可以把它变成一个私有的内部类,并使它成为静态内部类,但没有充分的理由这样做,或者可以将它设为私有并在SetUpSite实例之上创建它,但这是一个丑陋且不必要的组装机。

相关问题