Why main method is public static in Java
Javarevisited
Blog about Java programming language, FIX Protocol, Tibco Rendezvous and related Java technology stack.
MONDAY, DECEMBER 19, 2011
Why main method is public static in Java
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.
Difference between Java and Scala Programming
What is main method in Java?
10 HotSpot JVM Options, Every Java Programmer
Should Know
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.
Top 5 Java Programming Books for Developers
Top 10 JDBC Best Practices for Java programmers
Tips and Best practices to avoid
NullPointerException in Java
10 Object Oriented Design Principles Java
Programmer Should Know
Subscribe To This Blog Free
Posts
Comments
Signature of main method in Java
Main method has to strictly follow its syntax; other wise JVM will not be able to locate it and your program
will not run. Here is the exact signature of main method
Follow Us
Follow @javinpaul
public static void main(String args[])
This signature is classic signature and there from start of Java but with introduction of variable argument or
varargs in Java5 you can also declare main method in Java using varargs syntax as shown in below
example:
public static void main(String... args)
Remember varargs version of java main method will only work in Java 1.5 or later version. Apart from
public, static and void there are certain keywords like final, synchronized and strictfp which are permitted in
signature of java main method.
Why main method is static in Java
Now come to the main point "Why main method is static in Java", there are quite a few reasons
around but here are few reasons which make sense to me:
1. Since main method is static Java virtual Machine can call it without creating any instance of
class which contains main method.
2. Since C and C++ also has similar main method which serves as entry point for program execution,
following that convention will only help Java.
3. If main method were not declared static than JVM has to create instance of main Class and since
constructor can be overloaded and can have arguments there would not be any certain and consistent way
for JVM to find main method in Java.
4. Anything which is declared in class in Java comes under reference type and requires object to be created
before using them but static method and static data are loaded into separate memory inside JVM called
context which is created when a class is loaded. If main method is static than it will be loaded in JVM
context and are available to execution.
Why main mehtod is public in Java
Java specifies several access modifiers e.g. private, protected and public. Any method or variable which is
declared public in Java can be accessible from outside of that class. Since main method is public in
Java, JVM can easily access and execute it.
Why main method is void in Java
Since main method in Java is not supposed to return any value, its made void which simply means main is
not returning anything.
Summary:
1. Main method must be declared public, static and void in Java otherwise JVM will not able to run Java
program.
http://javarevisited.blogspot.in/2011/12/main-public-static-java-void-method-why.html[8/23/2014 8:38:29 AM]
Followers
Subscribe by email:
Subscribe
By Javin Paul
Loading
Blog Archive
2014
(
76
)
2013
(
136
)
2012
(
217
)
2011
(
145
)
December
(
28
)
Difference between Wait and Sleep , Yield in
Java
Step By Step guide to Read XML file in Java
Using ...
Error java.lang.UnsupportedClassVersionError:
Bad ...
Difference between DOM and SAX Parsers in
Java
How to read and write Images in java using
ImageIO...
How to Convert Map to List in Java Example
4 example to Iterate over HashMap, Hashtable
or an...
What is final in Java? Final variable , Method
and...
Difference between JRE JVM and JDK in Java
Program...
Why main method is public static in Java
2. JVM throws NoSuchMethodException:main if it doesn't find main method of predefined signature in
class which is provided to Java command. E.g. if you run java Helloworld than JVM will search for public
static void main String args[]) method in HelloWorld.class file.
3. Main method is entry point for any Core Java program. Execution starts from main method.
4. Main method is run by a special thread called "main" thread in Java. Your Java program will be running
until your main thread is running or any non-daemon thread spawned from main method is running.
5. When you see "Exception in Thread main e.g.
Exception in Thread main: Java.lang.NullPointerException it means Exception is thrown inside main
thread.
6. You can declare main method using varargs syntax from Java 1.5 onwards e.g.
public static void main(String... args)
7. Apart from static, void and public you can use final, synchronized and strictfp modifier in signature of
main method in Java.
How to Parse or Read XML File in Java >> XML
Tuto...
Checked vs Unchecked Exception in Java
Example
Observer design Pattern in Java with Real
world co...
How to change Tomcat default port 8080
What is Method Overloading and Overriding in
Java ...
Java.net.BindException: Address already in
use: JV...
Why main method is public static in Java
What is load-on-startup servlet element in
web.xml...
Is Apple IPhone4S Siri going to replace the
Google...
5 Example of kill command in UNIX and Linux
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.
Java TreeMap Tutorial: 10 Example of TreeMap
in Ja...
How to parse String to Enum in Java | Convert
Enum...
Top 5 FIX Protocol Data Dictionaries and Online
FI...
How to read and write in text file in Java
How to deal with Java.rmi.MarshalException:
CORBA ...
Some tutorials you may like
How to Split String in Java Program
How Substring in Java Works
How to debug Java Program in Eclipse Tips
String Replace Example in Java
Factory Pattern in Java with Example
Decorator Patten in Java with Example
Top 15 Thread interview questions in Java
Matt Cutt Revealing Google's Latest Algorithmic
Ch...
How to Create File and Directory in Java
Example -...
Java String Replace Example Tutorial
What is Factory method Design Pattern in Java
with...
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
)
December 20, 2011 at 2:03 AM
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
December 20, 2011 at 6:35 AM
References
Java API documentation JDK 6
Spring framework doc
Struts
Anonymous
said...
Following are valid main method signature:
JDK 7 API
public
public
public
public
MySQL
static
static
static
static
void main(String[] argument)
void main(String argument[])
void main(String... args)
synchronized void main(String... args)
http://javarevisited.blogspot.in/2011/12/main-public-static-java-void-method-why.html[8/23/2014 8:38:29 AM]
Linux
Why main method is public static in Java
public static strictfp void main(String... args)
public static final void main(String... args)
Eclipse
December 21, 2011 at 6:57 PM
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).
December 25, 2011 at 8:09 PM
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.
December 30, 2011 at 7:16 AM
Anonymous
said...
do you know how to pass arguments to main method in Java and how to get those arguments inside java
program ?
January 11, 2012 at 12:32 AM
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
January 14, 2012 at 6:47 AM
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
January 17, 2012 at 9:28 PM
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 ?
February 15, 2012 at 7:00 AM
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"
March 27, 2012 at 1:28 AM
Dimistri
said...
Can main method throw Exception in Java?
indeed main method can throw Exception both checked and unchecked.
Can main method be overloaded in java?
Yes main method in java can be overloaded but JVM will only invoke main method with standard signature.
Can main method be overridden in java?
Yes main method in java can be overridden and the class you passed to java command will be used to call main
method.
March 27, 2012 at 8:51 PM
Peter
said...
here are few more questions like why main is static:
Why run method in Java doesn't return any thing ?
http://javarevisited.blogspot.in/2011/12/main-public-static-java-void-method-why.html[8/23/2014 8:38:29 AM]
Copyright by Javin Paul 2012. Powered by Blogger.
Why main method is public static in Java
Why start method is used to start thread instead of run ?
By the way you can run Java class even without main method, just put your code inside static initializer block
and it will be run when class will be loaded.
May 20, 2012 at 11:37 PM
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.
August 14, 2012 at 5:51 AM
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.
August 14, 2012 at 6:00 AM
Anonymous
said...
Here is my list of frequently asked question on main in Java
1. Can we overload main method in Java?
2. Can we make main method final in Java ?
3. can we override main in Java ?
4. How to call a non static method from main in Java ?
5. can we make main method synchronized ?
These are good questions for any beginner to know more about Main method in Java.
November 5, 2012 at 8:14 PM
neha shirlekar
said...
can you please tell why it si mandatory to pass String args[] as a argument to main ()?
November 26, 2012 at 11:35 PM
Sasmita Nayak
said...
why other static methods are executed after main() method is executed even when main() method is also
static?
March 13, 2013 at 10:14 AM
Docket Smart
said...
This is really helpful for a beginner. Thanks
August 13, 2013 at 9:16 AM
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
August 21, 2013 at 1:32 AM
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"
August 26, 2013 at 2:53 AM
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
http://javarevisited.blogspot.in/2011/12/main-public-static-java-void-method-why.html[8/23/2014 8:38:29 AM]
Why main method is public static in Java
August 26, 2013 at 2:57 AM
Anonymous
said...
This is how you can invoke NON-STATIC method from main
public class TestMain {
public static void main(String[] args) {
TestMain test = new TestMain();
test.invokeMethodFromMain();
}
private void invokeMethodFromMain(){
System.out.println("inovked NON-STATIC method from main method");
}
}
April 1, 2014 at 4:01 AM
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
June 10, 2014 at 1:55 AM
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.
July 16, 2014 at 10:10 AM
Post a Comment
Newer
Post
Home
http://javarevisited.blogspot.in/2011/12/main-public-static-java-void-method-why.html[8/23/2014 8:38:29 AM]
Older
Post
Why main method is public static in Java
Subscribe to:
Post Comments
(
Atom
)
About
Me
Privacy
Policy
http://javarevisited.blogspot.in/2011/12/main-public-static-java-void-method-why.html[8/23/2014 8:38:29 AM]