|
JVM,JRE,Java Compiler FAQs-1
1)How can I write a program that takes command line input?
A: Java programs that take input from the command line declare a special
static method called main, which takes a String array as an argument and
returns void. The example program below loops through any arguments passed to
the program on the command line and lists their values.
2)What does public static void main(String[]) mean?
A: This is a special static method signature that is used to run Java programs
from a command line interface (CLI). There is nothing special about the method
itself, it is a standard Java method, but the Java interpreter is designed to
call this method when a class reference is given on the command line, as
below.
3)Why are command line arguments passed as a String?
A: Command line arguments are passed to the application's main method by the
Java runtime system before the application class or any supporting objects are
instantiated. It would be much more complex to define and construct arbitrary
object types to pass to the main method and primitive values alone are not
versatile enough to provide the range of input data that strings can. String
arguments can be parsed for primitive values and can also be used for
arbitrary text input, file and
URL references.
4)Why doesn't the main method throw an error with no arguments?
A: When you invoke the Java Virtual Machine on a class without any arguments,
the class' main method receives a String array of zero length. Thus, the
method signature is fulfilled. Provided the main method does not make any
reference to elements in the array, or checks the array length before doing
so, no exception will occur.
5)Why do we only use the main method to start a program?
A: The entry point method main is used to the provide a standard convention
for starting Java programs. The choice of the method name is somewhat
arbitrary, but is partly designed to avoid clashes with the Thread start() and
Runnable run() methods, for example.
6)Can the main method be overloaded?
A: Yes, any Java method can be overloaded, provided there is no final method
with the same signature already. The Java interpreter will only invoke the
standard entry point signature for the main method, with a string array
argument, but your application can call its own main method as required.
7)Can the main method be declared final?
A: Yes, the static void main(String[]) method can be declared final.
8)I get an exception if I remove the static modifier from main!
A: The static void main(String[]) method is a basic convention of the Java programming language
that provides an entry point into the runtime system. The main method must be
declared static because no objects exist when you first invoke the Java
Virtual Machine (JVM), so there are no references to instance methods. The JVM
creates the initial runtime environment in which this static method can be
called, if you remove the static modifier, it will throw a NoSuchMethodException.
9)How can the static main method use instance variables?
A: For very simple programs it is possible to write a main method that only
uses static variables and methods. For more complex systems, the main method
is used to create an instance of itself, or another primary class, as the
basis of the application. The primary application object reference uses
instance methods to create and interact with other objects, do the work and
return when the application terminates.
public class SimpleClass {
public void doSomething() {
// Instance method statements }
public static main(final String[] args) {
SimpleClass instance = new SimpleClass();
instance.doSomething(); } }
A: Yes, the main method can be called from a separate class. First you
must prepare the string array of arguments to pass to the method, then call
the method through a static reference to the host class, MaxFactors in the
example below.
String[] arguments = new String[] {"123"};
MaxFactors.main(arguments); |