Previous | Next | Trail Map | The Java Development Environment | Using System Resources


Using the System Class

Unlike most other classes, you don't instantiate the System class to use it.** All of System's variables and methods are class variables and class methods; they are declared static. For more information about class variables and methods and how they differ from instance variables and methods, refer to Static vs. Instance in 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:

System.out
You call class methods in a similar fashion but you need to indicate to the compiler that this is a method (not a variable). You do this with parentheses ( and ). Any arguments to the method go between the two parentheses; if there are no arguments, nothing appears between the parentheses. So, to reference the System's class method getProperty(), you append the method name (getProperty) to the end of the class name (System) separated by a period ('.') and you append the parenthesis and arguments after that like this:
System.getProperty(argument);
This small Java program uses the System class (twice) to retrieve the current user's name and display it.
class UserNameTest {
    public static void main(String args[]) {
	String name;
	name = System.getProperty("user.name");
	System.out.println(name);
    }
}
You'll notice that the program never instantiated a System object; it just referenced the getProperty() method and the out variable directly from the class.

System's getProperty() method used in the code sample searches the properties database for the property called user.name. System.out is a PrintStream that implements the standard output stream. The System.out.println() method prints it argument to the standard output stream. The next page of this lesson discusses System's standard output stream.

** To be more precise, you can't instantiate the System class--it's a final class and all of its constructors are private.


Previous | Next | Trail Map | The Java Development Environment | Using System Resources