[Solved] please explain how to implement main method in my code?


Suppose you have to create a program that prints a sum. You can create a file Sum.java with the Sum class inside it. Like this:

public class Sum {
    public int x;
    public int y;

    public Sum(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int sumMyNumbers() {
        return x + y;
    }

}

Now you can create a file named Main.java with your Main class that will be the entry point of your program and it could be like this:

public class Main {
    public static void main(String[] args) {
        // It will print the number 4 on your console
        System.out.println(new Sum(2, 2).sumMyNumbers());

        // Or like this:
        Sum mySum = new Sum(2,2);
        System.out.println(mySum.sumMyNumbers());

        // Or even like this:
        int i = new Sum(2, 2).sumMyNumbers();
        System.out.println(i);
    }
}

So your first mistake is that you are putting everything inside your main method.

0

solved please explain how to implement main method in my code?