简体   繁体   中英

How to use public void method in public static main

i need help, in a project i have a button set in public void method but i need to use it in my public static void main , here the problem, the method ask me to be static but in static i does not work at all and said my method is null.

public static void main(String[] args)
{
the app
}

@FXML
private AnchorPane Accueil;

@FXML
private AnchorPane Sport;

public void Entree() 
{
    Accueil.setVisible(false);
    Sport.setVisible(true);
}

so basically i want to do this

RunComplexe.Entree();

went i do this i got an error : Cannot make a static reference to the non-static method Entree() from the type RunComplexe

if u know how to fix it, tell me, thanks.

Create a new class as follows:

public class OtherClass {
        
    @FXML
    private AnchorPane Accueil;

    @FXML
    private AnchorPane Sport;

    public void entree() {
        Accueil.setVisible(false);
        Sport.setVisible(true);
    }
}

Now in your main method you need to create an object of such class so that you can call the method:

public static void main(String[] args) {
    OtherClass otherClass = new OtherClass();
    otherClass.entree();
}

As a side note, in Java, the standard is to have method names following snakeCase , so it should be entree() instead.

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