Why doesnot android application require main method
There is a main, but you didn't write it. The main is part of the Android framework. It will take the parameters and map them to an Activity or Service to run, then call the proper lifecycle functions. So you don't have to worry about it. Think of the onCreate of an Activity (or onStart of a service) replacing main.
How do Java programs run without defining the main method?
The main
method is only used when the Java Virtual Machine is executing your code. Code cannot be executed without a main
method but it can still be compiled.
When compiling code, you usually specify a set of files on the command line e.g.
javac MyClass1.java MyClass2.java
The Java compiler (javac
) examines each class you passed to it and compiles it into a .class file.
One reason Java source code may be missing a main
method is because it is designed to be used as a library, instead of being executed.
Something you may find interesting: although the source code compiled by the Java compiler does not need a main
method, the source code for the Java compiler itself does have a main
method.
Why does the first method to be called in Java have to be static but in android it doesn't?
These are two different scenarios. We have a general java language run by a java virtual machine. This jvm is designed to call a specific entry point for all applications. Thus this have to be as generic as possible.
Android runtime and many other like WARs, Applets, Servlets are "higher" apis. Thus they have all their specific entry points. Since these apis are already running within general jvm with an own main() method, they are able to implement more complex and domain specific entry points. Thus in this case the android runtime creates an object of a given class and invokes the methods designed by the api.
See also:
Java Web Application specify entry point
Entry point for Java applications: main(), init(), or run()?
And many other resource.
Starting Java Source File from Android Activity
You should try to separate the UI from the logic. Extract the part where you're computing something based on inputs from the part where you actually gather the input. That way, the logic methods (or classes) can be reused with several input-gathering methods.
For example, a Calculator class should not have the following methods:
/**
* asks for two numbers A and B, reads them from the keyboard, and displays the result
* of A^B on the screen
*/
public void power() {...}
/**
* asks for a number A, reads it from the keyboard, and displays the square root
* of A on the screen
*/
public void squareRoot() {...}
Instead, it should be separated into two classes:
public class Calculator {
/**
* returns a^b
*/
public double power(double a, double b) {...}
/**
* returns the square root of a
*/
public double squareRoot(double a) {...}
}
public class ConsoleCalculator {
private Calculator calculator;
public ConsoleCalculator(Calculator calculator) {
this.calculator = calculator;
}
/**
* asks for two numbers A and B, reads them from the keyboard, uses
* the calculator to compute A^B, and displays the result on the screen
*/
public void power() {...}
/**
* asks for a number A, reads it from the keyboard, uses the calculator to
* compute the square root of A and displays the result on the screen
*/
public void squareRoot() {...}
}
This way, it's easy to build an AndroidCalculator, because the hard-core math logic is encapsulated in a Calculator class, which doesn't care about where the inputs come from.
Run single java file with standard main(String [] args) method - Android Studio
If nothing else, you can make a quick JUnit test that calls your class's main method.
MainActivity not a static class and yet it executes without any explicit instantiation. How?
There is nothing wrong in asking basic questions... Actually, this type of pattern is not peculiar of Android, but happens whenever you have some framework in the middle. Some basic examples are java Applets and Servlets.
When you launch a Java app, you start a JVM and then you need to load something into it: so you need a static method (the main) because there are no objects (yet) living in the JVM that you can refer to.
If you have some sort of framework in the middle, it is the framework that will start the JVM and will start populating it with its own service objects: writing your code then means writing your own objects (which will be subclasses of given "template"). Your objects can then be injected (loaded) by the framework. The framework service objects manage the lifecycle of the injected objects by calling the lifecycle methods defined in the "template" superclass.
So for instance when you provide an applet to a browser, you do not launch a static main method: you rather only provide a subclass of java.applet.Applet that implements some instance methods which act as callback to manage the lifecycle (init, paint, stop...). It is the browser that will launch the JVM, instantiate what's needed for the launching an applet, load your applet and call it.
Similarly, with servlets you subclass the javax.servlet.http.HttpServlet class and implement some instance (non static) methods (doGet, doPost...). The Web container (e.g. Tomcat) will be in charge to launch the JVM, instantiate what's needed for launching a servlet, load your servlet and call it.
The pattern in Android is pretty much the same: what do you do is to create a subclass of android.app.Activity. When you launch an app, the system looks in the manifest to find out which activity should be started, then the "framework" loads it and calls its instance methods (onCreate, onPause, onResume...).
Related Topics
How to Enable and Disable a Component
Page Scroll When Soft Keyboard Popped Up
Android Device Is Not Connected to Usb for Debugging (Android Studio)
Refresh or Force Redraw the Fragment
Using Webview Sethttpauthusernamepassword
Android Sdk Tools Revision 22 Issue
How to Get Information About Android Battery Usage by Application
Android - Detect End of Long Press
How to Use Data Binding and Kotlin in Android Studio 3.0.0
Check Whether Lock Was Enabled or Not
Difference Between Addvalueeventlistener() and Addlistenerforsinglevalueevent() of Firebase
Adding Multiple Markers in Google Maps API V2 Android
Is Arm64-V8A Compatible with Armeabi-V7A
Why Is Calling Process.Killprocess(Process.Mypid()) a Bad Idea