RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect06a
what's a class; and new
exam results, too

(Time spent discussing exam problems/solutions, and learning style results.)
Note that an Excel spreadsheet itself is a programming language; we have functions, inputs, outputs, and (un)named constants. (Local variables are just extra cells. Excel makes it particularly easy to map a function to an entire list of inputs.)


Objects are: state + behaviors. (Book reading: Section 5.1)
(What is the java term for “state”? For “behaviors”?) Some people like the equality “state = fields = nouns; behaviors = methods = verbs”.
For each of the following, what fields might the class have? What methods?

By the way, we'll see later that fields can be not just a primitive or built-in type (like int or String, resp.), but also any class we define. So a class FootballGame might contain two fields of type FootballTeam, where each FootballTeam contains (in turn) both a name and many fields of type FootballPlayer, where FootballPlayer is a class which would contain … and so on.

Using classes gives us a natural way to think about how to organize our data (namely: in a way which reflects how we humans conceive/organize the topic). Once we have the data well-organized, then writing the methods follows.

new creates new objects

When you compile your class file (the blueprint), you don't actually create objects. The only way to create objects is by calling their constructor method. A constructor is a slightly odd method; it's called not by walking up to an object and asking it something (after all, there aren't any objects in existence when you start!), but rather by the keyword new:

new PizzaServer()
new Emcee()
We can type each of these new expressions into the code pad (presuming the corresponding classes (the blueprint) are defined is in the project). Note that in each case, there is a value returned from the new expression: the newly-created object. (In fact, in BlueJ, you can drag the small red box next to the return value, and put it onto the bench; BlueJ creates a variable-name to store this object, although conceptually that's not essential.)

Constructors with Arguments

We could add yet another field for PizzaServers, one which told which high school they graduated from. (High school graduation is a requirement for Krust-eaze Pizza Ink.) The problem is, what initial value should be given this field?

class PizzaServer {

  double balance = 0;
  String almaMater = /* ??? */;

  // ...other fields, and methods, go here ...

  }

One approach might be to say “well, set a PizzaServer's alma mater to "unknown", and then after creating a new PizzaServer, we'll hope that the person immediately calls setAlmaMater to be the correct value.”
But that's begging for trouble.

A better solution: Whenever somebody uses new to create a new object, force them to provide the high school at that moment. We do this by writing a constructor ourselves, which takes an argument:

PizzaServer( String _almaMater ) {
  this.setAlmaMater( _almaMater + " High School" );
  }
Note that there is nothing special about the parameter name. We choose a name which is closely related to the field name, and yet ever-so-slightly different. An underscore is considered a valid letter for names in Java, and is nothing privileged.

We already know that to call the constructor, we use new:

new PizzaServer( "South" )

PizzaServer jo;
jo = new PizzaServer( "North" );
When the constructor is called (via new), the almaMater field is initialized -- to South High School in the first call shown, and to North High School in the second call shown (where the resulting PizzaServer object happens to get stored in the variable named jo).

You might be wondering, why we have been calling new all semester long, passing it zero arguments, without ever writing a constructor. It turns out, if you don't write a constructor explicitly, then Java secretly writes a “zero-argument constructor” for you. We've been relying on that fact up 'til now, with PizzaServer and all our other classes.

There are two unusual things to remember, about the constructor's signature:

Moreover, the constructor is called differently than other methods: instead of using the method call object.methodName(inputs...), we use new methodName(inputs...) (where methodName is the constructor -- that is, the name is the same as the class name). If you think about it, we can't walk up to some object and ask it to call the constructor, because at the start of our program, no objects have even been created yet! So starting with “object dot” would be impossible. That's why java has the special keyword new

A constructor is a method whose purpose is to make sure all fields get initialized.
(The code inside the constructor runs immediately after fields are initialized.)

the truth about String (optional)

Something sneaky: When you write "hello", that's actually shorthand for new String("hello"). That's why we can call methods on Strings, and it's how we made Strings that had different states from each other.

Type each of the following in Code Pad. Explain what each line is doing.

PizzaServer jo;
jo = new PizzaServer("West");
jo.pizzaArea(20);


// We can have an object, even if we don't
// use a local variable to remember it for later:
(new PizzaServer("Skyline")).pizzaArea(20);
// Fine, although we now have no way to ask
// that particular PizzaServer any more questions.


String greeting;
greeting = new String("Howdy!");
greeting.length()

(new String("hi")).length()

"bye".length()    // Odd, but otherwise just like above.
                  // Relies on the fact that behind our backs,
                  // Java re-writes `"bye"` to be `new String("bye")`.

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2008, Ian Barland, Radford University
Last modified 2008.Feb.27 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme