the main class in a java source file?
im new to java.
the standard code that is created after i created a new project in netbeans is:
package helloworldapp;
public class Main {
public static void main(String[] args) {
int[] array = new int[10];
array[9] = 1;
System.out.println(array[9]);
}
}
so you see t开发者_如何学运维hat it uses the System class, but i dont see that class has been imported. Is there another code somewhere that does that? How can i use it if its not imported.
The System class is, along with everything else in java.lang
, imported automatically. Other classes you probably use that are automagically imported include String
and Exception
.
Additional info:
How can i use it if its not imported.
you can use any class in another package, without importing it, by using the fully qualified name. Example:
java.util.Date now = new java.util.Date();
instead of
import java.util.Date; // or java.util.*;
...
Date now = new Date();
Update:
import
is only used at compile time to determine the fully qualified name of classes.
System
is in the java.lang
package which is automatically available in all Java programs.
Directories containing all standard Java classes are part of the default search algorithm used by both the compiler (javac) and java commands. As already described in other answers, java.lang and java.util are examples of the System classes being automatically available for the code you're writing.
The JAR file containing these classes can be found on the currently used JDK installation directory of your machine
精彩评论