Topics Index

final, static and others >
Siva Nookala - 16 Mar 2016
Lets re-look at the Java Sample Program - Simple Hello World Program In Java which was discussed long time back. Now that we understand classes, methods and there return types, static methods, arrays and access specifiers, lets see how the simple java program which was very confusing then, will be very clear now.

class PrintHelloWorld
    public static void main(String args[])
        System.out.println("Hello World");
  • Why class: In Java, every line of code/statement should be included as part of a class, that is the reason, we have defined a class with the name of the program PrintHelloWorld.
  • Why main: When we run the program using java PrintHelloWorld it will look for the method main in the class PrintHelloWorld. The method main is the starting point of execution of the program.
  • Why static: If we want to call a method, we should have an object, since we can not create an object without going into main method, it will become a problem. If we make the main method as static then we can call it with out creating any object.
  • Why public: Since the main method should be accessible from outside of the program and probably from different packages, you need to make it public. If you make it private or protected or default, then the method can not be accessed from a different package.
  • Why void: Even if return any parameter from the main method, the program does not know what to do with it. If you want to return a process exit code, we should use the method System.exit and the not return the value from main method.
  • Why String[]: One way to pass parameters to java program, is to use the command line arguments. When we run the program with command line arguments, the arguments are converted into a String array and passed as a parameter to the main method.

Dependent Topics : Static Keyword In Java  Using private Keyword In Java For Access Control   Access Modifiers In Java  Java Methods  Java Class 

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App