简体   繁体   中英

Trying to call a method in a method from another class

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

I'm trying to call the method toonBoot from the class Boot. This should be done for every tweedeboot of the type Boot (same as the class) from the ArrayList boten. toonBoot prints a few lines of information (basically it's a number of coordinates).

For some reason, I always receive the error "non-static method toonBoot() cannot be referenced from a static context". What am I doing wrong? Thanks!

You have to call method on instance .

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

Where

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

See:

What you are doing

By calling the method from the Class name , you're telling the compiler that this method is a static method. That is, calling Boot.hello() that the method signature for hello() is something like:

public static void hello() {}

What you should do

Call from the object reference, or in this case tweedeboot . This tells the compiler that the method is either a static method or an instance method, and it will check the instance as well as the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM