Entry Point for Java Applications: Main(), Init(), or Run()

Entry point for Java applications: main(), init(), or run()?

This is a peculiar question because it's not supposed to be a matter of choice.

When you launch the JVM, you specify a class to run, and it is the main() of this class where your program starts.

By init(), I assume you mean the JApplet method. When an applet is launched in the browser, the init() method of the specified applet is executed as the first order of business.

By run(), I assume you mean the method of Runnable. This is the method invoked when a new thread is started.

  • main: program start
  • init: applet start
  • run: thread start

If Eclipse is running your run() method even though you have no main(), then it is doing something peculiar and non-standard, but not infeasible. Perhaps you should post a sample class that you've been running this way.

Java Entry Point

The basic concept is the main class is searched first and than and only than it is executed via main. So the first answer is NO. You cannot change the entry point.

Now in your code you have a static System.out.prinln block. In java, static contents are loaded when the class is loaded for the first time and they just have a single copy in the memory. So static block will be executed after the main block is found. Just try removing this main block and you will see the difference yourself

Which is the first method called when Java executes a program?

In addition to aioobes answer

A usual way to start a simple java program is to execute java like this:

java com.example.MyClass

com.example.MyClass (or your fully qualified class name) needs to have a main method with exactly this signature:

public static void main(String[] args)

(you're only allowed to change the name of the parameter, like arguments instead of args). The virtual machine will try to load the named class and try to invoke this static method which will "start the Java program".

When is java main method called?

main is a static method, the entry point for the program, and is called once (unless you explicitly call it), when the program starts, not for each object initialization.

Entry point for Java applet

According to your question "Entry point for java applet".

The life cycle of an applet is:

  1. init
  2. start
  3. stop
  4. destroy

So the entry point of an applet is init

How does the JVM use the Main method to start a Java program?

main doesn't need to be a keyword in java in order for the JVM to look for it at the start of execution. There is no conflict with other methods or variables also called main. This is simply how the JVM spec was designed. It was most likely borrowed from the c language.

Java Specification References: keywords, invoking main.

What is the use of the init() method in this code?

basically the init method setting up the recyclerview with its configuration

mRestaurantRecyclerView = (RecyclerView) findViewById(R.id.restaurant_recycler);

defining the recycler from xml and put it into variable mRestaurantRecyclerVIew

mRestaurantRecyclerView.setLayoutManager(new LinearLayoutManager(this));

setting up how the recyclerview is going to be displayed, later it should be defined in the project

mRestaurantRecyclerView.setHasFixedSize(true);

it's like the function said, the rv will have fixed size, you can also refer to this link for this particular function Understanding RecyclerView setHasFixedSize

 mRestaurantCollection = new ArrayList<>(); 

this mRestaurantCollection going to hold list of the data that is going to be dispayed in recyclerview.

mAdapter = new RestaurantAdapter(mRestaurantCollection, this);
mRestaurantRecyclerView.setAdapter(mAdapter);

last but not list, these 2 lines of codes is going to hook the data with recyclerview via adapter. first line is initiating the adapter inserting 2 parameters which is the data and the context, the second line telling the recyclerview, "hey i am your adapter, display this.".

PS : last but not list meant to be a joke. haha sry if its not funny.



Related Topics



Leave a reply



Submit