![]() ![]() ![]() ![]() |
Using System Resources |
Unlike most other classes, you don't instantiate the System class to use it. (To be more precise, you can't instantiate the System class--it's a final class and all of its constructors are private.)All of System's variables and methods are class variables and class methods--they are declared
static
. For a brief discussion about class variables and class methods and how they differ from instance variables and instance methods, refer to Static vs. Instancein the The Anatomy of a Java Application.
To use a class variable, you use it directly from the name of the class using Java's dot ('.') notation. For example, to reference the System's class variable
out
, you append the variable name (out
) to the end of the class name (System
) separated by a period ('.') like this:You call class methods in a similar fashion. For example, to call System's class methodSystem.outgetProperty()
, you append the method name to the end of the class name separated by a period ('.'). Any arguments to the method go between the two parentheses; if there are no arguments, nothing appears between the parentheses.This small Java program uses the System class (twice), first to retrieve the current user's name and then to display it.System.getProperty(argument);You'll notice that the program never instantiated a System object; it just referenced theclass UserNameTest { public static void main(String args[]) { String name; name = System.getProperty("user.name"); System.out.println(name); } }getProperty()
method and theout
variable directly from the class.System's
getProperty()
method used in the code sample searches the properties database for the property calleduser.name
. System Properties later in this lesson talks more about system properties and thegetProperty()
method.
System.out
is a PrintStream that implements the standard output stream. TheSystem.out.println()
method prints it argument to the standard output stream. The![]()
Using System Resources