11/3/11

Public static void main(String args[]){} : Explained


public static void main(String args[]){

}

Now lets understand why do we write the above statement, the way it is written above. 

Why not change it?

Before we move to the topic make sure you understand the Keyword Static, to learn about it here is our tutorial on Static Keyword in Java.

The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. (The opposite of public is private, which prevents a member from being used by code defined outside of its class.


In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made. The keyword void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values.

As stated, main( ) is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main. It is important to understand that the Java compiler will compile classes that do not contain a main( ) method. But the Java interpreter has no way to run these classes. So, if you had typed Main instead of main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main( ) method.

Any information that you need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters. If there are no parameters required for a given method, you still need to include the empty parentheses. In main( ), there is only one parameter, but a complicated one. String args[ ] declares a parameter named args, which is an array of instances of the class String. (Arrays are collections of similar objects.) Objects of type String store character strings. In this case, args receives any command-line arguments present when the program is executed.

Now since you have understood the meaning of public static void main(String[] args){} you should take a look at concepts on Classes and Objects and OOPs Concept. To know more about these you can click here

SHARE THIS POST:

Related Posts:

  • Java Tutorial : What is a Class? Java class is nothing but a template for object you are going to create or it’s a blue print by using this we create an object. In simple word we can say it’s a specification or a pattern which we define and every object we… Read More
  • OOPs Concept : Inheritance After discussing Encapsulation, now its time for 'Inheritance' as OOP Concept. Inheritance can be defined as the process where one object acquires the properties of another. With the use of inheritance the information is … Read More
  • OOPs Concept : Polymorphism Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any java object that can pass more t… Read More
  • Public static void main(String args[]){} : Explained public static void main(String args[]){ } Now lets understand why do we write the above statement, the way it is written above.  Why not change it? Before we move to the topic make sure you understand the Keywo… Read More
  • Good Practices : Programming Tips Java One of the Good practices while programming using Java is listed below. Consider Static Factory Methods instead of Constructors The normal way for a client to obtain an instance of itself is to provide a public const… Read More