RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect06a
Constructors with Arguments
lect06a

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()
new Dog()
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.almaMater = _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()

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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