HOW CAN I EXECUTE ANY PROGRAM IN JAVA WITHOUT MAIN METHOD?

Awadhendra Tiwari

Total Post:126

Points:882
Posted by  Awadhendra Tiwari
Java 
 1839  View(s)
Ratings:
Rate this:

Hi,

I have a  small problem while creating program. This might be a silly question as I am fresher in JAVA. I want to ask that
is it possible to run any program in java which have not main method. Like we can run some program in c and c++ which
doesent have main method. Is it possible in java.

Thanks.

  1. James Smith

    Post:48

    Points:336
    Re: How can I execute any program in java without main method?

    Hi awadhendra,

    You can use static block to perform this task and at the end of statements called System.exit(0) to avoid to generate an exception.

    class MainMethodNot
    {
        static
        {
            System.out.println("This java program have run without the run method");
            System.exit(0);
           
        }
    }

    Thanks.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!