You are on page 1of 5

CORE-JAVA

Importance of Main()method in JAVA:

The main intention of main() in java application is

1.To manage application login in java applications.

2.To define starting point and ending point to the application execution.

Syntax:

public static void main(String[] args)

----instructions-----

Note: main() method is not predefined method, it is not user defined method, it is a
conventional method implementation.

Q) What is the requirement to declare main() method" public"?

In java applications to strart applications execution jvm has to access main() method, to
access main() method scope must be available to jvm. To bring main() method scope to the
jvm we have to declare main()method as "public".

If we declare main() method as private then that main() method is available upto the
respective main class only, not to the jvm, so that jvm is unable to access main() method.

if we declare main() method as <default> then main() method is available upto the respective
package only, not to the jvm, so that jvm is unable to access main() method.

if we declare main() method as "protected " then main() method is available upto the present
package and upto the child classes available in the other packages, but not the jvm, so that
jvm is unable to access amin() method.

id we declare main method as "public " then main() method is available through out the java
application or through out the system including jvm, so that, jvm is able to access main()
method.

Note: If we declare main() method with out "public" then coplier will not raise any error, but in
jvm will provide the following messages.
JAVA6: Main method not public

JAVA7: Error: Main method not found in class Test, please defined

public static void main (....)

2Q)What is the requirement to declare main () method as "Static"?

IN java applications to start application execution , jvm has to access main() method. java has
prepared jvm in such away to access main() method by using class name only.

as per jvm internal implementation , about to access main() method must be declared as
Static". because in jvm JEEE technologies only static methods are eligible to access by using
class name.

Note: I f we declare main() method as static then complier will not raise any error, because
compiler will treat main() method as normal java method, we can declare normal java method
with out static keyword but jvm will provide the following exceptions.

java6: java no suchmethoderror

java7: errormain method is not static in class test please define the main method as :public
static void main(string array args...)

3Q)what is the requirement void as return type to main() method?

In java application jvm will create a thread to access main() method called as main thread.
Main thread will start application execution at starting point of the main() method and main
thread will terminate application execution at ending point of the main() method.

as per the above conversion we have provide the complete application logic inside man()
method that is we must start application login at starting point of the main() method and we
must terminate application login at ending point of the main() method, to preserve this java
conversion we must not return any value from main() method, for this we have to provide
void as return to the main() method.

Note: In java appli. if we declare main() method with out return type then compiler will not rise
any error, because compiler will create main() method as normal java method , we can
declare normal java method with out void return type and with some other return type like int,
float,.... but jvm will provide the following exceptions.

Java6: java.lang,no suchmethod error :main


Java7:Error main method must return a value of type void is class test, please define the main
method as : Public static void main (string[] args)

Note: While preparing java programming language , java creators have to give to name to the
main() method like " main" is the reflect its importance.

Parameters to the main() Method:


what is the requirement to provide parameters to the main() method?

In java application there are three ways to provide input to the

1. Static Input:

2.Dynamic Input

3.Commnad line Input

1. Static Input: If we provide input to the java prorgam at the time of writing program that
input is called as static input.

Ex:

Class A

int i=10;

int j=20;

void add()

system .out.println(i+j);

}}

Dynamic input:

if we provie input to the java program at the runtime that input is called as dynamic input.

D:\java10>javac add.java

D:\java10>java add
first value:10

second value:20

addition value :30

command line input: if we provide input to the java program along with java command on
command prompt then that input is called as command line input.

Ex:

D:\Java10>javac add.java

D:\java10>java add 10 20

Addition :30

If we provide command linne input then jvm will read command line input from command
prompt jvm will store all the command line input in the form of string[] and jvm will access
main() method by passing the generated string[] as parameter.

Therefore the main purpose of parameters to the main() method is to the store all command
line input in the form of string[] inorder to make available command line input to the java
program.