简体   繁体   中英

Calling a void method from a static main method

I'm trying to call a void method from my static main method. Here's what I have:

public static void main(String[] args){
    Scene scene = new Scene();
    Animate animate = new animate();
    animate();    //I've tried it by itself, but no luck
}
public void animate(){
    sun.slowMoveHorizontal(5000);
    moon.slowHorizontal(400);
}

If anyone could help it would be much appreciated. The text says, "Add a call to animate in your main method just below the line that creates the Scene object." If that helps.

Assuming Animate is a proper class, you need to call the instance method on an actual instance.

For example.

Animate a = new Animate();
a.animate();

Otherwise, check out the Scene class. Are you supposed to animate that?

Change:

public void animate(){

To:

public static void animate(){

You are trying to invoke a non-static method from a static one.


If animate() is an instance member, you instantiate the object and call animate() from that object:

public static void main(String[] args){
    Animation a = new Animation();
    a.animate();
}

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