Results 1 to 4 of 4

Thread: Procedure to run Java program without main method

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    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":

    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();
    }
    }

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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:
    class ABC
    {
    static
    {

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

    }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    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:
    static
    {
    System.out.println("Java program");
    System.out.print("program execute successfully");
    }
    System.exit();
    }

Similar Threads

  1. Replies: 5
    Last Post: 15-12-2010, 07:18 PM
  2. Explain main method in java
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 10-03-2010, 01:18 PM
  3. Why main method is static in java?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 04:20 PM
  4. What is method overriding and method overloading in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 08:20 AM
  5. What if we declare the main method as private?
    By Baran in forum Software Development
    Replies: 4
    Last Post: 25-02-2009, 07:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,638,444.59513 seconds with 17 queries