The Artima Developer Community
Sponsored Link

Java Buzz Forum
public static void main(String[] args)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
public static void main(String[] args) Posted: May 23, 2015 6:41 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: public static void main(String[] args)
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • main method is a standard method used by JVM to execute any java program(Java SE).
  • Lets see an example class without main method.

  1. package instanceofjava;
  2. public Demo{
  3.  
  4. }
  • This class will compile fine but while executing JVM fails to find the main method in that class
  • So it will throw an exception: NoSuchMethodError:main

  1. package instanceofjava;
  2. public Demo{
  3. public static void main(String [] args){
  4.  
  5. }
  • This program will compile and executed. Because at run time JVM calls the main method on that class.

Why main method is public?

  •  To call by JVM from anywhere main method should be public.
  • If JVM wants to call our method outside of our package then our class method should be public.

  1. package instanceofjava;
  2. public MainDemo{
  3. public static void main(String [] args){
  4.  
  5. }
  6. }

Why main method is static?

  • To execute main method without creating object then the main method oshould be static so that JVM will call main method by using class name itself.

Why main method return type is void?

  • main method wont return anything to JVM so its return type is void.

 Why the name main?

  • This is the name which is configured in the JVM.

Why the arguments String[] args?

  •  Command line arguments.
  • String array variable name args : by the naming conventions they named like that we can write any valid variable name.

Can we write static public void main(String [] args)?

  • Yes we can define main method like static public void main(String[] args){}
  •  Order of modifiers we can change.

  1. package instanceofjava;
  2. public MainDemo{
  3. static public void main(String [] args){
  4.  
  5. }
  6. }

What are the possible main method declaration with String[] args?

  • public static void main(String[] args){ }
  • public static void main(String []args){ }
  • public static void main(String args[] ){ }
  • public static void main(String... args){ } 
  • Instead of args can place valid java identifier.

Can we declared main method with final modifier?

  • Yes we can define main method with final modifier.

  1. package instanceofjava;
  2. public MainDemo{
  3. public static final void main(String [] args){
  4.  
  5. }

Main method with all possible modifiers:

 

  1. package instanceofjava;
  2. public MainDemo{
  3. static final synchronized public void main(String [] args){
  4.  
  5.  System.out.println("Main method");
  6.  
  7. }

Output:

  1. Main method

Overloading main method:

  • We can declare multiple main methods with same name as per overloading concept.  
  • So we can overload main method but every time JVM looks for main method with String[] args method.
  • Lets see an example program on main method overloading.
  • main(String[] args) method will be called automatically by JVM.
  • If we want to call other methods we need to call explicitly.

 

  1. package instanceofjava;
  2. public MainDemo{
  3. public static void main(String [] args){
  4.  
  5.  System.out.println("Main method String [] args");
  6.  
  7. public static void main(int[] args){
  8.  
  9.  System.out.println("Main method int[] args");
  10.  
  11. }
  12.  



Output:

  1. Main method String [] args

Main method Inheritance:

  • Inheritance concept is applicable for main method.
  • If a sub class not having main method and extending a super class which is having main method.
  • Then if we execute super class main method will be executed fine.
  • If we execute sub class which is not having main method also executed fine and super class main method will be called.

  1. package instanceofjava;
  2. public A{
  3. public static void main(String [] args){
  4.  
  5.  System.out.println("Main method of class A");
  6.  
  7.  



Output:

  1. Main method of class A

 

  1. package instanceofjava;
  2. public B extends A{
  3.  
Output:

  1. Main method of class A

What happen if both super and sub class having main method?

  • Actually its not overriding concept here. it is a method hiding concept in this scenario regarding with main method.

  1. package instanceofjava;
  2. public A{
  3. public static void main(String [] args){
  4.  
  5.  System.out.println("Main method of class A");
  6.  
  7.  

Output:

  1. Main method of class A


  1. package instanceofjava;
  2. public B extends A{
  3. public static void main(String [] args){
  4.  
  5.  System.out.println("Main method of class B");
  6.  
  7.  


Output:

  1. Main method of class B

Read: public static void main(String[] args)

Topic: Spring lookup-method Example Previous Topic   Next Topic Topic: Happy 20th Birthday Java!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use