Place your text ad here.
World class Hard Drive Recovery and renowned raid recovery services
WestNIC provides reliable web hosting services
Free software downloads and drivers download resources
This FAQ is part of the Code Style Help and FAQ section. Use the help request form below if your question is not answered here, but make sure you are asking the right question first.
main method declared public static void?
public static void main(String[]) mean?
main() method declared?
main method to start a program?
main method be overloaded?
String?
main method throw an error with no arguments?
main() method with a String argument?
*.java as an argument I get a list of all my source files!
main method be declared final?
static modifier from main!
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.
More details available to subscribers:
How can I write a program that takes command line input?
main method declared public static void?
A: The main method is declared public so that it is accessible as part of the public interface of the program. It is static because it must be called before the class that hosts the method is instantiated. It returns void because the Java interpreter does not expect to receive or process any output from the class; any output intended for end users will normally be sent to the system output and error streams, or via a graphical user interface or supplementary logging systems.
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.
More details available to subscribers:
What does public static void main(String[]) mean?
main() method declared?
A: The static void main(String[]) method can be declared in any Java class to create a running Java application. Where you choose to place the main method will depend on the nature and structure of your application. It is possible for numerous classes to have their own main entry point methods, but only one entry point can be used to start the program. An application will typically have a top level application class, an "Editor", "Viewer", "Server" or "Monitor" type that will be used to start and run the program, for example.
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.
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.
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.
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.
main() method with a String argument?
A: It is possible to change the name of the argument in the public static void main(String[]) method, but the argument type must be a string array. If you declare a String argument, it will make a valid method, but it cannot not be used as the standard entry point for your program, the Java runtime system would report an error.
A: The number of arguments that may be passed to a Java program will vary from one interpreter to another and will also be affected by the operating system's command interpreter. In a simple test, 169 arguments were passed to the Sun Java interpreter via a Windows batch script before it reported "The input line is too long". If your program requires a great number of input parameters, it would be better to pass a reference to a Java properties file on the command line and extract the input from that, as below.
More details available to subscribers:
How many arguments can be passed on the command line?
*.java as an argument I get a list of all my source files!
A: The behaviour you have identified is an implicit part of the way the java tool interacts with the host operating system. The java tool interprets the asterisk as a so called "wildcard" operator that represents a set of files. This is considered a convenience for Java programmers because it is an easy way to pass a series of file names as arguments to the main method. Putting a partial file path before the asterisk, or a file extension after it makes the file listing more specific, but you cannot not alter the fundamental behaviour.
It is necessary to enclose any argument that contains an asterisk in double quotes, to ensure it is not expanded to a series of file names. An alternative is to omit the asterisk in the command line and write your program in such a way that the asterisk is implicit and add it to the input arguments where applicable.
main method be declared final?
A: Yes, the static void main(String[]) method can be declared final.
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.
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();
}
}
main method from another class?
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);
| Front-end FAQs | Back-end FAQs | Learning Java |
|---|---|---|
See site help for questions about this site, our text ads and sponsored links services.