如何在另一个类中调用方法?

时间:2014-08-10 03:58:56

标签: java compiler-errors

我正在尝试在我的其他课程中调用statemotOfPhilosopy

有两个类:

头等课SetUpSite

package setupsite;

public class SetUpSite {
    public static void main(String[] args) {
        statementOfPhilosophy();
    }

    public static void statementOfPhilosophy() {
        System.out.println("Even Handlers Incoroporated is");
        System.out.println("dedicated to making your event");
        System.out.println("a most memorable one.");
    }
}

第二课CallingMethods

package callingmethods;

public class CallingMethods {
    public static void main(String[] args) {
        System.out.println("Calling method from another class:");
        SetUpSite.statementOfPhilosophy(); //Error here at the SetUpSite
    }
}

3 个答案:

答案 0 :(得分:1)

您忘记添加导入语句添加此行 :: import setupsite.SetUpSite


    package setupsite;
    public class SetUpSite {        
        public static void main(String[] args) {
            statementOfPhilosophy();
        }
        public static void statementOfPhilosophy()
        {
            System.out.println("Even Handlers Incoroporated is");
            System.out.println("dedicated to making your event");
            System.out.println("a most memorable one.");
        }         
    }

package callingmethods;
import setupsite.SetUpSite

public class CallingMethods {
    public static void main(String[] args) {
        System.out.println("Calling method from another class:");
            SetUpSite.statementOfPhilosophy(); 
    }
}

答案 1 :(得分:0)

您必须导入setupsite包。 您的代码将如下所示:

package callingmethods;
import setupsite.SetUpSite;

public class CallingMethods {


    public static void main(String[] args) {
        System.out.println("Calling method from another class:");
            SetUpSite.statementOfPhilosophy(); //Error here at the SetUpSite
    }

}

答案 2 :(得分:0)

你必须在import setupsite打包:

import setupsite;

然后试试。