0

I have been trying to make the Hello World Java application. But when I try to run the program it says that my selection has no main type. Here is my source code.

public class HelloWorldClass {

    System.out.println("Hello world!");
}
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

3

Because it has no main() method where the code would take the starting point. Use this

public class HelloWorldClass {
  public static void main(String[] args) {
    System.out.println("Hello world!"); 
  }
}

Please note there are some of the things you should always take a note of while programming. Which is also known as the syntax of the language.

Java requires you to have the class written as the FileName, HelloWorldClass is the name of the file of yours.

Then, any data type of it. In my case it is void which means it won't return anything to you in the end.

Also, you should write String[] args which is the Parameter to the method. I was last night trying to understand why I should write these? There is a method, which runs without these, but Java recommends you to add the parameters.

Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103
2

When running Java you need a main method so the compiler knows what to run:

public class HelloWorldClass {

 public static void main(String[] args)
        {
            System.out.println("Hello world!");
        }

}
user3574492
  • 6,225
  • 9
  • 52
  • 105