Program 6.9: An OOPTest that uses the constructor

Program 6.9 uses the constructor instead of the set methods to prepare the web site to be printed.

class OOPTest {

  public static void main(String args[]) {
    website w = new website("Cafe Au Lait", 
      "Really cool!");
If all you want to do is create new web sites and print them, you no longer need to know about the member variables name, url and description. All you need to know is how to construct a new website and how to print it.

You may ask whether the setURL, setDescription and setName methods are still needed since all three of these are now set in a constructor. The general answer to this question depends on the use to which the website class is to be put. The specific question is whether a website may need to be changed after it is created. If youıre using this class to create a database of web sites like that at Yahoo, for example, then it is entirely possible that the URL may change while the name and description remain the same or that the site may update its description so set methods are necessary for this class. However some other classes may not change after theyıre created; or , if they do change, theyıll represent a different object. The most common such class is String. You cannot change a Stringıs data without creating a new String.

Copyright 1996 Elliotte Rusty Harold
This Chapter