|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
 
This section explains how the "Hello World" application uses classes and objects. If you aren't familiar with object-oriented concepts, then you might find this section confusing. If so, feel free to skip ahead to the lesson Object-Oriented Programming Concepts
The "Hello World" application is about the simplest program you can write. It defines only one class. However, most programs you write will be more complex, requiring the creation of additional classes.
The "Hello World" application does use another class--the
Systemclass-- which is part of the API (application programming interface) provided with the Java platform. TheSystemclass provides system-independent access to system-dependent functionality. For information about theSystemclass, see Accessing System Resources.
The bold code in the following listing illustrates the use of a class variable of the
Systemclass, and of an instance method./** * The HelloWorldApp class implements an application that * simply displays "Hello World!" to the standard output. */ class HelloWorldApp { public static void main(String[] args) { System.out.println("Hello World!"); //Display the string. } }
Let's take a look at the first segment of the statement:The constructSystem.out.println("Hello World!");System.outis the full name of theoutvariable in theSystemclass. Notice how in this case,outis referred to directly from the class name. It is a class variable--a variable associated with the class rather than with an instance of the class-- and it is accessed directly. Like variables, methods can also be associated with the class. In both cases this is done with thestatickeyword, in the source code for the class where that variable or method is defined. For classes provided by the Java platform, you can determine whether or not a particular member is static by referring to the API specification for that class. If you see the static keyword, that variable or method belongs to the class. The following link shows the API specification for theSystemclass.
To refer to class variables and methods, join the class name and the name of the class method or class variable together with a dot (".").
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'soutvariable is a class variable, it refers to an instance of thePrintStreamclass (a class provided with the Java development environment) that implements the standard output stream.When the
Systemclass is loaded into the application, it instantiatesPrintStreamand assigns the newPrintStreamobject to theoutclass 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:
This line of code displays "Hello World!" to the application's standard output stream.System.out.println("Hello World!");
 
|      | Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form | 
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.