Trick Boss
Trick Boss

Reputation: 31

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!

Upvotes: 1

Views: 64

Answers (2)

Suresh Atta
Suresh Atta

Reputation: 122026

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:

Upvotes: 4

christopher
christopher

Reputation: 27356

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.

Upvotes: 1

Related Questions