genjava / tips / java explanation


package com.generationjava.util;
// above is the package name. It maps to the directory structure and 
// allows many classes with the same name to exist on the same system.
// It means that this file needs to go in 
// com/generationjava/util/SystemPropertiesLister.java

// we import Properties so we can use them lower down.
// note, we don't need to import java.lang. System is in java.lang.
import java.util.Properties;

/**
 * Prints out the system properties.
 */
public class SystemPropertiesLister {

    /**
     * Standard main method. Usually run via command line, but doesn't 
     * have to be.
     */
    // This method is called in the class if you invoke the class like:
    //   java com.generationjava.util.SystemPropertiesLister
    // Anything you put after the classname gets put into the String array.
    // I usually call the array variable 'args'. 
    public static void main(String[] args) {

        // first we get the system's properties.
	// System is a class which represents the current system.
	// As code can only run on one system at a time, 
	// its methods are implemented as static methods. This 
	// means you can use the class directly and not have to 
	// create objects. Using static classes is useful, but 
	// is something which should rarely be necessary.
        Properties sysprops = System.getProperties();

        // Properties happens to have a method named list.
	//    void Properties.list(java.io.PrintStream)
	// This method returns nothing (void).
	// It takes an argument which is an object of the 
	// PrintStream class, located in the java.io package.
	// Any packages likes java.*** are standard ones.
	// The System class provides two static variables, 
	// one is 'out' and the other is 'err'. They represent 
	// the STDOUT and STDERR on the machine. 
	// 'out' is for normal output, and 'err' is for errors/debugging.
	// Note, these are static variables of System, this means 
	// we don't need to "instantiate a System object" but 
	// the 'err' variable itself IS an object. System "instantiated" 
	// it at some point.
	sysprops.list(System.err);
    }

}