Friday, July 28, 2017

Java Program Structure


Java Program Structure for developing java application.

package details
class className
{
            Data Members
            User_defined Methods

public static void main(String[] args)
{
            Block of Staments
}
}

Implementing a Simple Java Program

Creating hello java example

import java.io;
class Hello
 {
 public static void main(String[] args)
{
 System.out.println("Hello Java");
System.out.println("My First Java Program");
}

}

To compile: javac Hello.java

To execute: java Hello


Understanding Hello java program

1.      class keyword is used to declare a class in java.
2.      public keyword is an access modifier which represents visibility, it means it is visible to all.
3.      static is a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.
4.      void is the return type of the method, it means it doesn't return any value.
5.      main represents startup of the program.
6.      String[] args is used for command line argument. 
7.      System.out.println() is used to print statements. 

Valid java main method signature
  1. public static void main(String[] args)  
  2. public static void main(String []args)  
  3. public static void main(String args[])  
  4. public static void main(String... args)  
  5. static public void main(String[] args)  
  6. public static final void main(String[] args)  
  7. final public static void main(String[] args)   
Invalid java main method signature
  1. public void main(String[] args)  
  2. static void main(String[] args)  
  3. public void static main(String[] args)  
  4. abstract public static void main(String[] args)

No comments:

Post a Comment