Why Main Method Is Public Static in Java PDF
Why Main Method Is Public Static in Java PDF
Javarevisited
Blog about Java programming language, FIX Protocol, Tibco Rendezvous and related Java technology stack.
Best of Javarevisited
How Android works, Introduction for Java
Programmers
Main method in Java is the first programming method a Java programmer knows when he starts learning
Java programming language.have you ever thought about why main method in Java is public, static
and void, of-course Yes, since most of us first learn C and C++ than we move to Java in our programming
path we familiar with main method but in Java main method is slightly different it doesn't return any value
like in C it returns int, main method is public static and void Why? In this post we will try to find answer of
these questions and have an idea of one of the most popular questions in Java why main method is
declared Static.
Main method in Java is entry point for any core Java program. Remember we are not talking about Servlet,
MIDlet or any other container managed Java program where life cycle methods are provided to control the
execution. In core Java program, execution starts from main method when you type java main-class-name,
JVM search for public static void main(String args[]) method in that class and if it doesn't find that
method it throws error NoSuchMethodError:main and terminates.
Posts
Comments
Follow Us
Follow @javinpaul
Followers
Subscribe by email:
Subscribe
By Javin Paul
Loading
Blog Archive
2014
(
76
)
2013
(
136
)
2012
(
217
)
2011
(
145
)
December
(
28
)
Error java.lang.UnsupportedClassVersionError:
Bad ...
8. Main method in Java can be overloaded like any other method in Java but JVM will only call main method
with specified signature specified above.
9. You can use throws clause in signature of main method and can throw any checked or unchecked
Exception.
10. Static initializer block is executed even before JVM calls main method. They are executed when a Class
is loaded into Memory by JVM.
November
(
14
)
October
(
14
)
Posted by
Javin Paul
at
4:38 AM
Labels:
core java
,
core java interview question
Location:
United States
September
(
22
)
August
(
11
)
July
(
7
)
June
(
9
)
24 comments
:
May
(
6
)
Neeraj
said...
April
(
10
)
Though I understand why main is static in Java and why main is publicI didn't understand the point on making
main method void in Java, Why not it could be int or boolean to return whether it ran successfully or not. We
already have main method in C++ and C which returns int.
March
(
4
)
February
(
10
)
January
(
10
)
2010
(
33
)
extremejava
said...
one more point is that one can play with the argument to main method as:
String[] args,String args[]
Also sometimes we get confused that the following is not a valid main method but it is:
public static void main(String[] test123){...}
Java Interview : Database SQL and JDBC
Connection and Datasource in Java Applications
References
Java API documentation JDK 6
Spring framework doc
Struts
Anonymous
said...
JDK 7 API
public
public
public
public
MySQL
static
static
static
static
Linux
Eclipse
jQuery
Ashutosh Agarwal
said...
@Neeraj
We have servlet & other as managed java objects. The java runtime environment provides many facilities like
memory allocation & garbage collection etc. Our main application is executed by the JRE process. The process
can return the status to the OS as zero or some other integer to return its status. Your application can return
the value or JVM itself. Thus it might have make sense to assign the value by using System.exit(1).
farhan khwaja
said...
Nice explanation of 'public static void main(Strin args[])'. here is my post on the same 'public static void
main(String args[]): Explained
here is point too on main:
The main method can be overloaded.
Anonymous
said...
do you know how to pass arguments to main method in Java and how to get those arguments inside java
program ?
iTfinGer Ode
said...
even though main method should not return values,it can use empty return statement.
ex return; allowed
but return somevalue; not allowed
Anonymous
said...
Java Main method can also throw any Exception or Error in its throws clause, following main method in java is
perfectly legal:
public static void main(String args[]) throws AssertionError
Radhika
said...
Can we declare main method private? this was the question asked to me. What will happen if we make main
method private in Java, i said we can not access it from outside of Class since its private and also can not run
Java program, was that correct answer ?
Anonymous
said...
Can we throw exception from main method in Java ? I am asking both checked and unchecked Exception. Also
when do we get error "no main class found your program will terminate"
Dimistri
said...
Peter
said...
Aster
said...
JVM does not find for the Main Method its the JNI. Secondly your logic that why main is public has no reason.
Since the class is never instantiated it doesn't matter is its public or not.
Main method is entry point for any Core Java program, "Any"...no
should be "public, static and void in Java", no you can write your own JVM and instead have some other return
type and signature.
You should realize your responsibility, many newbies may read your article and may take your wrong
information ar true.
Javin Paul
said...
In order to access the Class must be public. All JVM are written using JVM specification and I have never heard
of any JVM changing signature of Main method, if you have better reason than please share.
Anonymous
said...
neha shirlekar
said...
can you please tell why it si mandatory to pass String args[] as a argument to main ()?
Sasmita Nayak
said...
why other static methods are executed after main() method is executed even when main() method is also
static?
Docket Smart
said...
Anonymous
said...
does any one knows how to call a non static method from main method in Java? I am getting "cannot make a
static reference to the non-static method" error while calling a non static method, declared in same class.
please help
Anonymous
said...
for exmaple my main fuction is not static how can i call main function i.e can i call main function after creating
an object of class in which it is defined and calling it keeping in view that main function is not defined "Static"
Anonymous
said...
similarly i want that my functions in one class can be called by any other class that i define, without creating
object of class whose function i want to be called how do i accomplish this
keep in mind no friend function is allowed
Anonymous
said...
Moaz Amin
said...
hello sir you tesch us that main method must be public because public method can access easily outside the
class i agree with you but non specifire methods are also like a public but this code is not run
static void main(String s[]) why it is also accessable outside the class
Omkar
said...
If public modifier is removed (made default or made private)then JVM reports following: Main method not public.
If arguments are modified like (String[]args, int i)...to test overloading....then it reports following: Exception in thread "main" java.lang.NoSuchMethodError: main
This leads to a conclusion that this is a STANDARD FORMAT, JVM looks for, the "public static void main(String[]
args)" helps it have an entry point. That is precisely the reason why a thread created when main method
executes is called "main" who sets this name to the thread ? .... we developers never set it explicitly but JVM
must have done in implicitly, which also means this contract of "public static void main(String[] args)" if
followed then JVM will follow it too and the program will execute else it will not.
Post a Comment
Newer
Post
Home
Older
Post
Subscribe to:
Post Comments
(
Atom
)
About
Me
Privacy
Policy