Java – main() method

Static Members:

  • Defining class member with static keyword.
  • Static members are:
    • Static main() method
    • Static Block
    • Static Method
    • Static Variable

Static main() method:

  • Java program execution starts with main() method.
  • JVM invokes the main() method when we run the program.
  • We must define main() method as
public static void main(String[] args)
Or
public static void main(String args[])

Program to display Hello World on Console:

public class Code
{
            public static void main(String[] args){
                        System.out.println(“Hello World”);
            }
}

Class without main(): We can define class without main() method but we cannot run that class.

public class Code
{
            void fun(){
                        System.out.println(“fun…”);
            }
}

Runtime Error: No main() method in class Code. Please define main() method

Java Application Structure:

  • We define java application in multiple java source files (.java files).
  • Source file contains classes.
  • Every application has only one execution point(main).
  • Main() method belongs to single class from which application execution starts
Scroll to Top