Program 6.2: Create and Print a Website

Program 6.2 creates a new website, sets its fields, and prints the result.

class OOPTest {

  public static void main(String args[]) {
    
    website w = new website();
    
    w.name = "Cafe Au Lait";
    w.url = "http://sunsite.unc.edu/javafaq/";
    w.description = "Really cool!";
    
    System.out.println(w.name + " at " + w.url + 
      " is " + w.description);
    
  }
  
}
Program 6.2 requires not just the OOPTest class but also the website class. To make them work together put Program 6.1 in a file called website.java. Put Program 6.2 in a file called OOPTest.java. Put both these files in the same directory. Then compile both files in the usual way. Finally run OOPTest. Note that website does not have a main method so you cannot run it. It can exist only when called by other programs that do have main methods. Here's the output.

% javac website.java
% javac OOPTest.java
% java OOPTest
Cafe Au Lait at http://sunsite.unc.edu/javafaq/ is Really cool!
%
Many of the applications you write from now on will use multiple classes. It is customary in Java to put every class in its own file. In Chapter 21 you'll learn how to use packages to organize your commonly used classes in different directories. For now keep all your .java source code and .class byte code files in one directory.


Copyright 1996, 1998 Elliotte Rusty Harold
elharo@sunsite.unc.edu
This Chapter
Examples
Home