Thursday, December 12, 2019

Can You Lot Operate Coffee Programme Without Principal Method?

First affair Java programmers learns is that they demand a primary method to run, but when they drib dead to whatever Interview or college viva too asked can yous run a Java plan without a primary method, they surprised similar hell. Well, in that place are truly dissimilar types of execution model available inwards Java, for example, Applets which run on browser doesn't bring primary method, instead they bring life-cycle methods similar init(), start() too stop(), which controls their execution. Since Applet is a Java program, yous tin response this query inwards Yes. Similarly, nosotros bring Servlet, which runs inwards a Servlet container, comes equally bundled inwards a web server similar Tomcat, or Jetty. Servlet too plant on callback mechanism, it has methods similar init(), service() too destroy(). Container calls init() if Servlet is offset fourth dimension loaded into memory, calls service() if in that place is a asking to procedure too calls destroy() earlier removing it from memory.

Since Servlet is too a Java program, nosotros tin say that it runs without primary method. Third 1 on this category is MIDlet, which runs on mobile devices e.g. Nokia, Samsung too others. MIDlet has life-cycle methods similar startApp(), pauseApp() too destroyApp(), to start, recess too shut-down mobile applications.

Ironically J2ME has app inwards their meat method, but it was made pop past times Android too iPhone. Since MIDlets are too Java program, yous tin say they run without primary method.

Now, if Interviewer persist yous amongst something similar this, Can yous run a Java plan without primary method inwards Core Java, non on managed surroundings similar the instance of Applet, Servlet too MIDlet. Answer is NO, Why? I volition explicate that inwards side past times side paragraph.

By the way, this is 1 of the many questions related to primary method inwards Java, yous tin come across the amount listing of interview query on primary method here.




Java plan without Main method

Many Java programmer gives yous response that they tin run Java plan without primary method past times writing code inwards static initializer block, which is one-half true. Yes, code written inwards static initializer block is executed earlier calling primary method, but yous won't live on able to run a course of written report past times using Java command, or Eclipse or anything else, until it got  public static void main(String args[]) method on it. If yous endeavour to run such programs, yous volition acquire next mistake :

Error: Main method non institute inwards course of written report JavaAppWithoutMain, delight define the main
 method as:   populace static void main(String[] args)

 First affair Java programmers learns is that they demand a primary method to run Can You Run Java Program Without Main Method?










Though yous tin run a Java plan amongst empty Main method, inwards which instance entirely code executed volition live on from static initializer block. Following is a uncomplicated Java plan amongst simply about code written on static initializer block, including a impress statement, a variable initialization too starting a thread. As presently equally yous take away the primary method, it volition compile fine but volition throw inwards a higher house error, when yous endeavour to run this plan from command line.

/**  * Java application to demonstrate whether yous tin run a plan without a primary method  * or not.  * @author http://java67.blogspot.com  */ public class JavaAppWithoutMain {      static {         System.out.println("HelloWorld, Java progarm without primary method");         int x = 20; // Can initialize static variables         System.out.println("Variable x : " + x);          Thread t = new Thread() {             @Override             public void run() {                 System.out.println("Started thread from static initializer block");     System.out.println("Thread Finished");             }         };         t.start();     }      public static void main(String args[]) {           // Empty primary method     }  }

Here is how the output expect like, when yous run this plan from command prompt amongst empty primary method.

 First affair Java programmers learns is that they demand a primary method to run Can You Run Java Program Without Main Method?









That's all nearly whether yous tin run a Java plan without primary method inwards Java or not. In short, Yes, yous tin run a Java plan without primary method inwards a managed surroundings e.g. Applet, Servlet, too MIDlet, which runs nether command of browser, server too mobile device, but can't run a meat Java plan without populace static void main(string args[]) method. JVM volition non allow yous to execute those methods.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

No comments:

Post a Comment