In Java why we write public static void main(String args[])
why not main()?
Answers were Sorted based on User's Feedback
Answer / ankur
in java we write public static void main(String args[])
instead of just main because:....
public.....The main method must be public because it is
call from outside the class. It is called by jvm.
static....The main method must be static because without
creating an instace jvm can call it. If the main method is
non static then it is must to create an instance of the
class.
void....Main method doesn't return anything therefore it is
void also.
(String args[])....It is used for receiving any arbitirary
number of arguments and save it in the array.
Is This Answer Correct ? | 429 Yes | 20 No |
Answer / ravi
static because once the class is loaded the main() will be
invoked first.
void because main()is not returning any value.
Is This Answer Correct ? | 228 Yes | 99 No |
Answer / sumalatha
yes in every progrm in jave the main is static.because when
the program will start ,thit means runnig then the main is
also one method it was called first that means without
instans creations the main is called.thet is the reason we
are put as main is static method(static is used for without
instance creat u call it).void means it does not reaturn
any value
Is This Answer Correct ? | 148 Yes | 43 No |
Answer / sanjay gupta
public static void main()
1> Main() is the entry point of the program. Rather we can
say it is the main gate to enter inside the apartment. It
should be public because the compiler need to enter inside
the method (The guest need to access the apartment to reach
the security guard).
Public--> Compile whos is from another instance need to
access this method.
Statis --> Since main method is written inside a class and
to access a method of a class we need to create an object
of that class first. Since main() is the method which
compiler nned to call before creating any object of that
class we need to declare main as static. Static methods can
be called directely by using the class name. Thats why the
neame of the file should be same as the name of the class
contain main() method.
Void--> Since the method can return any type of valu or it
might not return anything also so the comiler is designed
in such a way that it should not take any return value so
we declare main as void type.
For further information please reach me at
sanjaygupt1011@gmail.com
Thanks~
Happy Programming...
Is This Answer Correct ? | 88 Yes | 12 No |
Answer / anjani kumar jha
Use of Public-----There is always only one main class that
is one file having only one main file. Since that main file
is used anywhere means might be in another package so there
is a public use of keyword that is public keyword is
required for main function
Static:-----Since compiler in java always look the main
function and when compiler always calls the main so main
class is loaded in first time call for that static keyword
is used.
you can use
1)public void main(String args[])
it compiles file but wont run\
2) void main(String args[])
it compiles file but wont run
3)void main(String args[])
it compiles file but wont run
Is This Answer Correct ? | 87 Yes | 37 No |
Answer / manikandan
in java , the entry point for execution is Main(),
it is tha first method to be invoked .
so PUBLIC is declared for no duplicates should occur and for
global declaration .
VOID , the main method will not return any value . So void
is declared .
Finally STATIC , in java everything is accssed through
objects . But here main() is the first method to be invoked
. STATIC is defined for this purpose , If a method is
declared as STATIC it can called without any objects
reference .
So the Main() in java is defined as
public static void main(String args[])
Is This Answer Correct ? | 50 Yes | 15 No |
Answer / angela
when we run java program, jvm internally calls "main" method.
jvm is built in such a way that it searches for the entire
signature of "main" method i.e public static void
main(String a[])
Example program :
----------------------
class Demo
{
public static void main(String a[])
{ }
}
String a[] -> is used to pass values at runtime.
(c:/>java Demo hai)
public -> providing accessibility for outside code (jvm).
static -> without creating an object calling "main" method
with the class name
c:/>javac Demo.java
c:/>java Demo
(here java internally calls jvm and passes class name
Demo, then jvm loads Demo class and calls Demo.main()
without creating object )
void -> does not return any value because jvm simply calls
main() method and does not have a variable to assign return
value.
Is This Answer Correct ? | 23 Yes | 1 No |
Answer / prabhakant prabhakar rasal
We write public because to access main() method outside the
class.
Why static because to invoke main() method before creating
any object, so it simple to compiler that to identify the
main() method.
void, it represents that it does not returns any value.
string args[]
There are two main reasons to write string args[]
1. TO identify the program
2. To get command_line arguments
Is This Answer Correct ? | 27 Yes | 13 No |
1. public - Access Specifier, properties and methods can
access anywhere, any package.
2. main() - The Java environment starts the execution of a
program from main() method.
3. void - The main() method is not return anything. So the
return type of the main() method must be void.
4. static - The jave environment able to call the main()
method without creating an instance of the class. So its
declared as static
5...(String args[]) -- In java, whatever we give, it will
takes as String. So we are giving the command line argumet
as String.
i hope it will help u..
Shivadasan
Coromandel Infotech
Is This Answer Correct ? | 16 Yes | 4 No |
Answer / suresh.t.c
public static void main(String arg[])
public for accessing all object
static for to call main method without instance
void main for not returning values for avoid compilation errors
String arg[] is ,in input takes as strings so takes multiple inputs declared String with array of arg[]
Is This Answer Correct ? | 14 Yes | 3 No |
What is java object name?
Why is stringbuffer called mutable?
When a thread is executing synchronized methods , then is it possible to execute other synchronized methods simultaneously by other threads?
Explain the significance of listiterator.
How does arrays sort work in java?
Given a singly linked list, find the middle of the list in a single traversal without using temporary variable.
Why do we use regex?
When is the garbage collection used in Java?
different between exception and error? explaim check and uncheck exception
What is the basically use of finally while we know it is always executed but why?
Given: 10. interface A { void x(); } 11. class B implements A { public void x() { } public voidy() { } } 12. class C extends B { public void x() {} } And: 20. java.util.List list = new java.util.ArrayList(); 21. list.add(new B()); 22. list.add(new C()); 23. for (A a:list) { 24. a.x(); 25. a.y();; 26. } What is the result? 1 Compilation fails because of an error in line 25. 2 The code runs with no output. 3 An exception is thrown at runtime. 4 Compilation fails because of an error in line 20.
What is a treemap in java?