About Main method.

About Main method.
When we execute a java program, JVM first starts the execution process from the main method i.e, public static void main(Strings[] args) method.

But why Public Static Void?

before going to the detailed understanding of this method, you need to understand what are modifiers.

Modifiers: These are the set of keywords specified to the at variable declaration  or for method definition to change the their meaning.

There are two kinds of modifiers in Java.

1. Access Modifiers:
2. Non Access Modifiers. 

Access Modifiers: These Modifiers will provide the access level permissions to the class,variables and method levels.

Access Modifiers are four types in Java

1. public
2. private
3. protected

Public : by the terminology itself says that it is public means visible to all. so this keyword makes members access to public, you can access this members anywhere and modify them (until and unless the members are final).

Where can a public keyword  be used.
1.  To a class
2.  To a constructor
3.  To an instance variable.
4.  To a Method

Private: when you want to make the members of a class not be visible to public, but can only visible and accessed within the same class.

Where can a private keyword  be used.

1.  To a constructor
2.  To an instance variable.
3.  To a Method.
4. To inner classes

Protected : when you want to make members visible and accessed by all classes with in the package and through inheritance for the classes outside the package, then we go with the protected keyword.

Where can a protected keyword  be used.

1. Instance Variable.
2. Methods
3. Constructors.
4. Inner classes.

There is one more access modifier called default, When we don't provide any access modifier to the members, then JVM treats them as default, this members are accessible only within the classes of same package.

Non-Access Modifiers:

This modifiers doesn't work on the accessibility of the the members, rather provide some special functionalities.

There are 5 non access modifiers

1. final
2. static
3. synchronized.
4. transient
5. volatile

Final: this keyword once given to a variable, its value is fixed.

Syntax :
final String a="someValue";

Now the value of a will never be changed.

When the keyword is given to a method, we cannot override the method.

syntax:
public final void calculate(){
------
------
}

Static:This keyword when given to a variable or the method we can access them even without creating the object of the class.

for example:

class StaticClass{
public static staticVariable=1;

 public static String staticMethod(){
  return staticVariable;
 }
}

class TestClass{
 public static void main(String args[]){
  //We can the static variable and staticMethod of StaticClass without creating the object.
  System.out.println(StaticClass.staticVariable);
  System.out.println(StaticClass.staticMethod());
 }
}


One more use of static keyword is to create static blocks. These blocks are class initializers i.e, when ever class is loaded this block is executed. In the entire runtime jvm loads only loads any class for one time, if at required it reuses it from the method area where the classes are loaded already.

lets create a class StaticClass.java

 package com.learning.nonaccessmodifiers;

public class StaticClass {
 public static String staticVariable;

 static {
  staticVariable="HelloWorld";
  System.out.println("Initialization of  static variables"
    + "in static block : " + staticVariable);
 }

 public static String print() {
  return staticVariable;
 }
}

Now lets run this code in the Testing Class
package com.learning.nonaccessmodifiers;

public class TestingClass {
 public static void main(String[] args) { 
  StaticClass class1=new StaticClass();
  System.out.println(class1.staticVariable);
  StaticClass class2=new StaticClass();
  System.out.println(class2.staticVariable);
  StaticClass class3=new StaticClass();
  System.out.println(class3.print());  
 }
}

Output:
Initialization of  static variables in static block : HelloWorld
HelloWorld
HelloWorld
HelloWorld

We can see clearly now . even though the application came across the StaticClass.java three times but static block executed only once.

Synchronized: This keyword comes into picture, when we deal with threads. only one thread can access the synchronized block at a time. (Will discuss more about this keyword multi threading).

Transient: When an Object if needed to transported over a network, then the object need to be serialized i.e, object is converted to serial bytes and transferred through the network protocols,.  Transient key if given to the member variable. that variable wont be serialized.

example: transient int variable=10;

now after object passes through the network and access this variable, it will hold the default value of it type. i.e, 0.

Volatile: this keyword also comes into picture, where we deal with threads and no cache mechanism.
i.e, if a variable is assigned.. the thread cannot assign this value in cache. it should always access it through main memory.(will discuss more on this key word in multi threading).

So by now we should know, why the JVM checks for main method which is public static void,

Public because, the code you execute is at x location and should be visible to the JVM. i.e, jvm  doesn't belong to the same package of the source code,

Static :  JVM doesn't know how to create objects, rather it should have a way to execute the method, so it is declared as static.

Void:  JVM doesn't expect any return value from main method it  just executes. so that's why its void.

main: as per java specification, the name of the method should be always main, we can modify the name, to some other name we want by modifying the source of the jvm.

String[] args or String... args or String args[] : these are the runtim Permalink e arguments which are passed while executing  the code, which are used by main method.


Now, Is the structure of the main method is fixed? is it always be public static void main(String args[])?no,
 we can slightly modify the structure,

for example:

static public void man(String[] args), is it valid? yes it is valid.

we are allowed to use some keywords to this method,like synchronized, final, strictfp

so it can look like this public static synchronized final strictfp void main(String args[])