Void methods cannot return a value

前端 未结 10 2199
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 01:38

I\'m following the CS106A lectures online. I\'m going through the code on Lecture 12, but it\'s giving me errors in Eclipse.

This is my code. It seems the error is beca

10条回答
  •  盖世英雄少女心
    2021-01-23 01:44

    string[] args in main method is used to follow the prototype defined by the technology. If you dont used it then JVM will considered it as a simple method. Also as this is prototype then remember technology used 0 byte of an array. for ex:

                   class MainDemo{
                        public static void main(){
                        System.out.println("I am main method without arg");
                    }
                    public static void main(String[] args){
                            main();   //As method is static so no need to create an object
                        System.out.println("MainDemo");
                    }
                    }
    

    O/p for above will give:

     I am main method without arg 
     MainDemo
    

    Regarding your code, you cannot define/declare any method inside main method. Use it outside main method but within class.

提交回复
热议问题