Previous | Next | Trail Map | Essential Java Classes | Accessing System Resources

Using the System Class

Unlike most other classes, you don't instantiate the System class to use it. To be more precise, you cannot 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 complete discussion about class variables and class methods and how they differ from instance variables and instance methods, refer to Instance and Class Members(in the Learning the Java Language trail) in the Objects, Classes, and Interfaces lesson.

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 to the class name separated by a period, like this:

System.out
You call class methods in a similar fashion. For example, to call System's getProperty method, you append the method name to the end of the class name separated by a period:
System.getProperty(argument);
The following small Java program uses the System class twice, first to retrieve the current user's name and then to 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 instantiates a System object. It just references the getProperty method and the out variable directly from the class.

The code sample use System's getProperty method to search the properties database for the property called "user.name." System Properties later in this lesson talks more about system properties and the getProperty method.

The code sample also uses System.out, a PrintStream that implements the standard output stream. The println method prints its argument to the standard output stream. The next page of this lesson discusses the standard output stream and the other two standard I/O streams provided by the System class.


Previous | Next | Trail Map | Essential Java Classes | Accessing System Resources