简体   繁体   中英

Why eclipse show me error in the line where i have written public static void main(String[] args)

So why public static void main(String[] args) I got an error. what can i do to resolve it?

package linkedList;

public class HackerRank {

    public class Solution {

        // Complete the aVeryBigSum function below.
        public long aVeryBigSum(long[] ar) {
            long a=0;
            for(int i=0;i<ar.length;i++){
                a=ar[i]+a;
            }

            return a;
        }


        public static void main(String[] args) { ///why this line is not correct 
            Solution s= new Solution();
            long[] ar= {10000,20000,30000};

            System.out.println(s.aVeryBigSum(ar));

        }
    }
}

There is another possible solution, taking the nested Solution class out of the HackerRank class, as I see you are not doing anything with it at the moment.

public class Solution {

    // Complete the aVeryBigSum function below.
    public long aVeryBigSum(long[] ar) {
        long a = 0;
        for (int i = 0; i < ar.length; i++) {
            a = ar[i] + a;
        }
        return a;
    }

    public static void main(String[] args) { 
        Solution s = new Solution();
        long[] ar = { 10000, 20000, 30000 };

        System.out.println(s.aVeryBigSum(ar));
    }
}

This makes sure that your static main method works.

You can't access a static method in a non-static class. There are 2 possible solutions for this problem: - 1. Make Solution static

public static class Solution {

    public static void main(String[] args) {
    //...
    }

}

- 2. Remove the static from the main method

public class Solution {

    public void main(String[] args) {
    //...
    }

}

So why public static void main(String[] args) I got an error. what can i do to resolve it?

package linkedList;

public class HackerRank {

    public class Solution {

        // Complete the aVeryBigSum function below.
        public long aVeryBigSum(long[] ar) {
            long a=0;
            for(int i=0;i<ar.length;i++){
                a=ar[i]+a;
            }

            return a;
        }


        public static void main(String[] args) { ///why this line is not correct 
            Solution s= new Solution();
            long[] ar= {10000,20000,30000};

            System.out.println(s.aVeryBigSum(ar));

        }
    }
}

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