Hello guys, the an initial thing Java programmers discover is the they need a main technique to run, yet when they walk to any Interview or college viva and ask have the right to run a Java program without a key method, they room surprised prefer hell. Well, there room actually different types of execution models obtainable in Java; because that example, Applets, which run on the internet browser don"t have the main method. Instead, they have life-cycle methods like init(), start() and stop(), which controls their execution. Since Applet is a Java program, you have the right to answer this inquiry is Yes. Similarly, we have Servlet, which operation in a Servlet container, comes together bundled in a internet server like Tomcat, or Jetty. The Servlet likewise works on call back mechanism, it has methods like init(), service(), and destroy(). Container call init() if Servlet is a first-time loaded into memory, call service() if over there is a inquiry to process, and also calls destroy() prior to removing that from memory.Since Servlet is additionally a Java program, we deserve to say that it operation without the main method. The 3rd one in this classification is MIDlet, which runs on mobile devices like Nokia, Samsung, and also others. MIDlet has life-cycle approaches like startApp(), pauseApp() and also destroyApp(), to start, pause and also shut-down cell phone applications.

You are watching: No main methods applets or midlets

Ironically J2ME has an app in its main point method, yet it to be made renowned by Android and also iPhone. Due to the fact that MIDlets are additionally Java programs, you have the right to say they operation without the key method.Now, if the Interviewer persists through something favor this, have the right to you operation a Java regimen without the main technique in core Java, no on the managed environment choose the situation of Applet, Servlet, and also MIDlet. The prize is NO, Why? i will explain that in the following paragraph.By the way, if girlfriend are new to the Java world and want to find out Java in-depth, I indicate you check out these free Java courses for beginners and experienced. This consists of several free courses to learn key topics choose core Java, Multithreading, collections, socket programming, and also more.

Java regimen without the main method

Many Java programmer provides you answer that they have the right to run a Java regimen without a main an approach by writing code in static initializer block, i m sorry is fifty percent true. Yes, code created in static initializer block is executed before calling the main method, but you won"t be able to run a class by using Java command, or Eclipse, or something else, until it obtained public static void main(String args<>) method on it.If you shot to run such programs, girlfriend will gain the complying with error :Error: Main technique not found in class JavaAppWithoutMain, please define the main
method as public static void main(String<> args)

Though you deserve to run a Java routine with an empty key method, in which case only code executed will be from the revolution initializer block. Following is a an easy Java program with part code composed on a static initializer block, consisting of a print statement, change initialization, and starting a thread.As soon as you remove the main method, it will compile fine however will litter the over error when you shot to operation this regime from the command line.By the way, this is one of the plenty of questions concerned the main an approach in Java; you can see various other questions for your interview preparation then I imply you inspect out this Java Interview Courses
, which consists of important object and an ext than 200+ Interview Questions and also Answers because that beginners and sufficient to crack any Java interview.

/** * Java application to demonstrate whether you can run a program without a main technique * or not. *
author http://beer-selection.com.blogspot.com */public class JavaAppWithoutMain static System.out.println("HelloWorld, Java progarm without key method"); int x = 20; // deserve to initialize revolution variables System.out.println("Variable x : " + x); object t = new Thread()
Override windy void run() System.out.println("Started thread from static initializer block"); System.out.println("Thread Finished"); ; t.start(); public static void main(String args<>) // north main an approach
Here is how the calculation looks like once you operation this routine from the command prompt with an empty key method.


That"s all about whether you have the right to run a Java routine without the main technique in Java or not. In short, Yes, you deserve to run a Java regime without a main method in a regulated environment likeApplet, Servlet, and MIDlet, which operation under manage of browser, server, and also mobile device, but can"t operation a core Java program without public revolution void main(string args<>)method. JVM will not enable you come execute those methods.

See more: What Is Another Name For A Muscle Cell Definition And Examples

OtherProgrammingArticles You might Like
to ExploreThanks for analysis this article so far. If you prefer this article, then please share it v your friends and also colleagues. If you have any type of questions or feedback, then you re welcome drop a note.P. S. - If you are brand-new to Object-Oriented Programming and also looking for some complimentary courses to find out Object-Oriented Programming climate you can additionally see this perform of my favorite best process to learn Java virtual for beginners.