Plzzzzzzzzzzzzzzzzzzzzzzzzzzzz helpppppppppppp
100 points plzzzzzzz don't spam
What is the significance of the main method in JAVA?
Anyone can write a program without main method and it will compile without error
Suppose: see it for your self and compile it is error free
class anything
{
void help()
{
System.out.println("Hello");
}
}
Then why we use the main method?
Answers
Answered by
1
Main method does have its significance.It is compulsory for a Java program to have a main method.It is the entry point of the program.
However it is not necessary for certain compiler like BlueJ with version less than 4
Difference of Main method from other methods:
- It can only have void return type as it can not return any value.
- It is the entry point of a program
Similarities of main method and other methods
- Main method can be overloaded as any other method
- It has got a return type i.e void
- It works as a normal function in most other ways.
Hope this helps : )
Answered by
1
main method is starting point of your stand alone java application.Having said that it means that if we have a Java project which has many classes in it with lot of code(business logic),you need some starting point such that your code starts executing.main method is this starting point,which then can call other classes code.
Lets take an example to check how main method gets called and how it calls other method
package com.test.main;
public class TestClass {
public static void main(String[] args) {
System.out.println("In TestClass");
System.out.println(new TestClass1().display()); // calling TestClass1 display method
}
}
package com.test.main;
public class TestClass1 {
public String display(){
return "In TestClass1";
}
}
Let us run TestClass from command prompt or from within eclipse
command prompt :
java TestClass
Eclipse : Right click on class having main method or right click on project name ,then select Run as -> Java Applicaiton
Output :
In TestClass
In TestClass1
Additional Info :
1) If you right click on a class which does not have main method ,eclipse will not show you Run as->Java application option.
2) If you remove any of public,static,void or arguments of main method,it will not be identified by JVM, but it will be considered as overload of main method.
Question : In "public static void main(String [ ] args)", what do"static ", "main", and "(String [ ] args)" mean?
Answer :
static :
As main method is invoked by JVM and by that time(when JVM invokes main method) no object of class in which main method is defined exists,there should be some way to call main method.This is the reason main method has been forced by java specifications to be static.Now as static methods belong to class(and not objects),it can be called by JVM using class name.
main :
This is name of method.Name has to be main to be identified by JVM as startup method.
String[] args :
main method accepts array of String.These are called command line arguments.
Consider following example :
package com.test.main;
public class TestClass {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println("In TestClass")
}
}
So if you are executing java program from command prompt as below :
java TestClass "hello"
Output will be :
hello
in TestClass
If you want to pass more than 1 argument,you can do like
Consider following example :
package com.test.main;
public class TestClass {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println("In TestClass")
}
}
java TestClass "hello" "Quora"
and output will be
hello
Quora
in TestClass
mark as BRAINLIST answer...
Lets take an example to check how main method gets called and how it calls other method
package com.test.main;
public class TestClass {
public static void main(String[] args) {
System.out.println("In TestClass");
System.out.println(new TestClass1().display()); // calling TestClass1 display method
}
}
package com.test.main;
public class TestClass1 {
public String display(){
return "In TestClass1";
}
}
Let us run TestClass from command prompt or from within eclipse
command prompt :
java TestClass
Eclipse : Right click on class having main method or right click on project name ,then select Run as -> Java Applicaiton
Output :
In TestClass
In TestClass1
Additional Info :
1) If you right click on a class which does not have main method ,eclipse will not show you Run as->Java application option.
2) If you remove any of public,static,void or arguments of main method,it will not be identified by JVM, but it will be considered as overload of main method.
Question : In "public static void main(String [ ] args)", what do"static ", "main", and "(String [ ] args)" mean?
Answer :
static :
As main method is invoked by JVM and by that time(when JVM invokes main method) no object of class in which main method is defined exists,there should be some way to call main method.This is the reason main method has been forced by java specifications to be static.Now as static methods belong to class(and not objects),it can be called by JVM using class name.
main :
This is name of method.Name has to be main to be identified by JVM as startup method.
String[] args :
main method accepts array of String.These are called command line arguments.
Consider following example :
package com.test.main;
public class TestClass {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println("In TestClass")
}
}
So if you are executing java program from command prompt as below :
java TestClass "hello"
Output will be :
hello
in TestClass
If you want to pass more than 1 argument,you can do like
Consider following example :
package com.test.main;
public class TestClass {
public static void main(String[] args) {
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println("In TestClass")
}
}
java TestClass "hello" "Quora"
and output will be
hello
Quora
in TestClass
mark as BRAINLIST answer...
Similar questions