简体   繁体   中英

Calling a void method in the same class

so I have a method

public void arin(int failure, boolean crash, boolean breakdown, double arinEngine, double arinComputer,
        int arinmorale, double arinRocket, String name, int choice, int fixAttempts) {

}

which is empty right now, and I want to call it into my main method. I have tried using run, using return, but nothing is working. I made it into a static and tried run.arin(); but it said run cant be resolved to a variable.

Create a class object or make the method static

public class YourClassName {

    public static void main(String[] args) {
        YourClassName test = new YourClassName();
        test.arin(0, false, false, 0.0, 0.0, 0, 0.0, "", 0, 0);
    }

    public void arin(int failure, boolean crash, boolean breakdown, double arinEngine, double arinComputer,
            int arinmorale, double arinRocket, String name, int choice, int fixAttempts) {

    }
}

You would call the method by writing arin() but things inside the parenthesis. ie arin(5, true, true, 2.1, 2.2, 6, 3.3, "car", 1, 7);

Your main method and this void method are in the same class you do not need to create an object for that class. You can just call it in the main method like I have above.

If you have your static method in the same class that main method, you can use (run) it just by calling it:

public class YourClass {

    public static void main(String[] args) {
        arin(0, false, false, 0.0, 0.0, 0, 0.0, "", 0, 0);
    }

    public static void arin(int failure, boolean crash, boolean breakdown, double arinEngine, double arinComputer,
        int arinmorale, double arinRocket, String name, int choice, int fixAttempts) {

    }
}

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