|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Unlike most other classes, you don't instantiate theSystemclass to use it. To be more precise, you cannot instantiate theSystemclass--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 declaredstatic. For a complete discussion about class variables and class methods and how they differ from instance variables and instance methods, refer to Understanding Instance and Class Membersin 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 theSystem's class variableout, you append the variable name to the class name separated by a period, like this:You call class methods in a similar fashion. For example, to callSystem.outSystem'sgetPropertymethod, you append the method name to the end of the class name separated by a period:The following small Java program uses theSystem.getProperty(argument);Systemclass twice, first to retrieve the current user's name and then to display it.You'll notice that the program never instantiates aclass UserNameTest { public static void main(String[] args) { String name; name = System.getProperty("user.name"); System.out.println(name); } }Systemobject. It just references thegetPropertymethod and theoutvariable directly from the class.The code sample use
System'sgetPropertymethod to search the properties database for the property called "user.name." System Properties later in this lesson talks more about system properties and thegetPropertymethod.The code sample also uses
System.out, aPrintStreamthat implements the standard output stream. Theprintlnmethod 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 theSystemclass.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.