试图从另一个类调用方法中的方法

时间:2013-10-24 14:51:28

标签: java

public void toonBoten()
    {
        for(Boot tweedeboot: boten)
        {
            Boot.toonBoot();
        }
    }

我正在尝试从类Boot调用方法toonBoot。应该对ArrayList boten中Boot类型(与类相同)的每个tweedeboot进行此操作。 toonBoot打印几行信息(基本上是一些坐标)。

由于某种原因,我总是收到错误“非静态方法toonBoot()无法从静态上下文中引用”。 我究竟做错了什么? 谢谢!

2 个答案:

答案 0 :(得分:4)

您必须在instance上调用方法。

public void toonBoten()
    {
        for(Boot tweedeboot: boten)
        {
            tweedeboot.toonBoot();
        }
    }

哪里

  Boot.toonBoot(); //means toonBoot() is a static method in Boot class

请参阅:

答案 1 :(得分:1)

你在做什么

通过从Class name调用方法,您告诉编译器此方法是static方法。也就是说,调用Boot.hello() hello()的方法签名是这样的:

public static void hello() {}

你应该做什么

从对象引用调用,或者在本例中为tweedeboot。这告诉编译器该方法是static方法或instance方法,它将检查实例以及类。

相关问题