|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
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 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"; } |
We already know that to call the constructor, we use new:
new PizzaServer( "South" ) PizzaServer jo; jo = new PizzaServer( "North" ); |
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:
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.)
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() |
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2007, Ian Barland, Radford University Last modified 2007.Aug.27 (Mon) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |