A structure of java program: – A java program contains at least on class and this class is called main method class. If more than one classes used with a java source program, except one class, other classes are called sub classes and one class is called main method class because this class control remaining sub classes. Name of java source file must be exactly same as name of main method class. A main method class can be declared in the following format –

Class <Class name>

Save this source file with ClassName.java extension. A class can be declaring with in pair of curly braces. Inside the curly braces, actual program can be written with main method. Its main method format is –

public static void main(String Args[])

A main method can be defined with different keyword. Function of these keyword are as fallows –

  1. Public: – The keyword public is an access specifier that declares the main method as unprotected and there for making it accessible to all other classes. This is similar to the C++ public modifier.
  2. Static: – The next keyword is static, which declares this method as one that belongs to entire class and not a part of any object of the class. The keyword main must always be declared as keyword static since the interpreter uses this method before any objects are created.
  3. Void: – This keyword is a modifier and it states that the main method does not return any value.
  4. Main: – It define the main method with keyword main. Conceptually, this is similar to the main() function in C or C++. Every java application must include the main() method. This is starting point for the interpreter to begin the execution of the program. A java application can have number of classes but only one of them must include a main() method initiate the execution and show that this class is called main method class.
  5. String args[]: – This is an argument of main method and is used as an object of the main method. Because various type of date in java source code are treated as string argument and show that it is necessary to add this argument with main method.

Structure of java source code –

class <Class Name>
{
                public static void main(String args[])
                {
                                Main Body;
                }
}
Advertisements