Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Procedure to run Java program without main method

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 26-11-2009
Member
 
Join Date: Nov 2009
Posts: 57
Procedure to run Java program without main method
  

Hi All,

I have read in lots of JAVA programming book that each JAVA program should have "main method", As execution of the program starts from the "main method" of the program. And we can't ignore main method

I want to know the is it possible to create & execute JAVA program code without "main method" ? Please suggest me....


Last edited by Sheena_thakkar : 26-11-2009 at 02:15 PM.
Reply With Quote
  #2  
Old 26-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: Procedure to run Java program without main method

Yes, It is possible to create Java program code without "main method".

See below example, Try to run this program it's running successfully without "main method":

Quote:
public class NoMain_Demo
{
static
{
System.out.println("Program without main method");
System.exit(0);
}
}

public class NoMain_demoTest
{
public static void main(String[] args)
{
new NoMain_Demo();
}
}
Reply With Quote
  #3  
Old 26-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Procedure to run Java program without main method

Hi friends,

By using the method known as "System.exit(0)" within the "Static Block" we can execute Java code without "main method". See something below can help you:

Program:
Quote:
class ABC
{
static
{

System.out.println("Java without main() method");
System.exit(0);

}
}
Reply With Quote
  #4  
Old 26-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: Procedure to run Java program without main method

Hi,

According to the my Java knowledge we are able to run the java program without the "main() method". It's done using "Static block", As it's execution is done before "main() method". So when you run the java program it will first execute the content from the static block and the execution will stop after the "System.exit() method". Please refer the below example of the static block:

Code:
Quote:
static
{
System.out.println("Java program");
System.out.print("program execute successfully");
}
System.exit();
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Procedure to run Java program without main method"
Thread Thread Starter Forum Replies Last Post
During bootup I get this error message Error invoking method: main (java.lang.string) Karsten M Windows Software 5 15-12-2010 07:18 PM
Explain main method in java Angelica Maria Software Development 5 10-03-2010 01:18 PM
Why main method is static in java? MKAIF Software Development 5 06-03-2010 04:20 PM
What is method overriding and method overloading in java beelow Software Development 3 17-11-2009 08:20 AM
What if we declare the main method as private? Baran Software Development 4 25-02-2009 07:35 PM


All times are GMT +5.5. The time now is 07:50 AM.