![]() ![]() ![]() ![]() |
The "Hello World" Application |
The "Hello World" application is about the simplest Java program you can write that actually does something. Because it is such a simple program, it doesn't need to define any classes except for HelloWorldApp. However, most programs that you write will be more complex and require you to write other classes and supporting Java code.class HelloWorldApp { public static void main (String args[]) { System.out.println("Hello World!"); } }The "Hello World" application does use another class--the System class--that is provided with the Java development environment. The System class provides system-independent access to system-dependent functionality. For information about the System class, see Using System Resources.
![]()
The implementation of the
main()
method illustrates the use of a class variable and of an instance method.Using a Class Method or Variable
Let's take a look at the first segment of the statement:The constructSystem.out.println("Hello World!");System.out
is the full name of theout
variable in the System class. Notice that the application never instantiates the System class and thatout
is referred to directly from the class name. This is becauseout
is a class variable--a variable associated with the class rather than with an instance of the class. You can also associate methods with a class--class methods.To refer to class variables and methods, you join the class name and the name of the class method or class variable together with a period (".").
Using an Instance Method or Variable
Methods and variables that are not class methods or class variables are known as instance methods and instance variables. To refer to instance methods and variables, you must reference the methods and variables from an object.While System's
out
variable is a class variable, it refers to an instance of the PrintStream class (a class provided with the Java development environment) that implements the standard output stream.When the System class is loaded into the application, it instantiates PrintStream and assigns the new PrintStream object to the
out
class variable. Now that you have an instance of a class, you can call one of its instance methods:As you can see, you refer to instance methods and variables similarly to the way you refer to class methods and variables. You join an object reference (System.out.println("Hello World!");out
) and the name of the instance method or variable (println
) together with a period (".").The Java compiler allows you to cascade references to class and instance methods and variables together, resulting in constructs like the one that appears in the sample program:
System.out.println("Hello World!");Summary
A class method or class variable is associated with a particular class. An instance method or instance variable is associated with a particular object (an instance of a class). [This used to say that class members occur once per class, and instance members occur once per instance of a class, but "occur" seems very vague to me. How can we get the point across?]
![]() ![]() ![]() ![]() |
The "Hello World" Application |