Previous | Next | Trail Map | Writing Java Programs | Table of Contents


Setting Program Attributes

When your Java programs run, they run within some environment. That is, the program is running in an environment where there's a user, a host machine, a current directory, and user preferences for window border color, font, and font size and other environmental attributes. In addition to these system (or runtime) environment attributes that describe the system environment for the program, your application may also set up certain application-specific attributes that are configurable. Application attributes are often called preferences and can often allow the user to configure various start up options, preferred window size, or whatever.

A program typically needs this information about the system and application environment to make decisions about how to do something, or what to do. Also, a program may wish to modify certain attributes itself, or allow a user to change them. Thus a program needs to be able to read and write various system attributes and program-specific attributes. Java programs can manage these attributes through three mechanisms: properties, application command line arguments, and applet parameters.

Properties are used to define environmental attributes on a persistent basis. That is, properties are in effect for every invocation of a program. In contrast, command line arguments are used to set or modify properties on a non-persistent basis for Java applications. That is, you use command line arguments to set one or more attributes for just one invocation of a program. Parameters are similar to command line arguments but are used with applets, use them to set one or more attributes for a single invocation of an applet.


Note about terminology: The term properties is used synonymously with the term attributes because properties in your Java program represent a set of attributes. The term command line arguments is never a synonym to the term attributes. You just use command line arguments to set or modify properties.

In Java, attributes are represented by the Properties class in the java.util package. A Properties object contains a set of key/value pairs. The key/value pairs are like dictionary entries: the key is the word, and the value is the definition.

[PENDING: picture of a properties list.]

Both the key and the value are Unicode strings. For example, os.name is the key for one of Java's default system properties--its value contains the name of the current operating system. You use a key to look up a property in the properties list and get its value. On my system, when I look up the os.name property, its value is Solaris. Yours will likely be different.

Properties specific to your Java application are maintained by your application. System properties are maintained by the java.lang.System class. For information about system properties, refer to System Properties(in the Writing Java Programs trail) in the Using System Resources lesson.

Properties

Use properties to define environmental attributes on a persistent basis. That is, use properties when they need to be in effect for each invocation of a program.

Application Command Line Arguments

Use command line arguments to define or modify environmental attributes on a non-persistent basis. That is, use command line arguments to change one or more attributes for just one invocation of a program.

Applet Parameters

Use applet parameters to define or modify environmental attributes on a non-persistent basis for applets. That is, use parameters to set one or more attributes for a single invocation of an applet. For more information about applet parameters, see Defining and Using Applet Parameters(in the Writing Applets trail)


Previous | Next | Trail Map | Writing Java Programs | Table of Contents