如何在java中调用非静态方法

时间:2015-04-11 17:00:45

标签: java methods call

我想在一个新方法中调用两个非静态方法。我该怎么做?

我试过.methodName()但是当我尝试编译时,我收到一条错误消息,说"找不到符号 - 方法methodName()"

1 个答案:

答案 0 :(得分:0)

public class TestClass {


    public String firstMethod()
    {
        return "first Method";
    }

    public String secondMethod()
    {
        return "Second Method";
    }

    public static void main(String[] args)
    {
        TestClass object = new TestClass(); // create a object for class `TestClass`
        System.out.println(object.firstMethod()); // access the non-static method by `classname.methodName` 
        System.out.println(object.secondMethod());          
    }

}

输出:

first Method
Second Method