|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
class PizzaServer { double salary = 5.15; // In $/hr. boolean isManager = false; double balance = 0.0; String almaMater; // Name of the highschool graduated from. /** The PizzaServer constructor. * @param _almaMater The highschool graduated from. * Don't include a suffix " High School"; that is * automatically added. * Example: * new PizzaServer( "Radford" ) */ PizzaServer( String _almaMater ) { this.almaMater = _almaMater + " High School"; } /* method definitions omitted ... */ } |
The syntax for a constructor's signature:
Signatureconstructor ::= Nameclass ( paramater-declaration-list ) constructor-call ::= new Nameclass( argument-list ) |
There are two unusual things about the constructor's signature:
What exactly1 happens, when you call a constructor, like new Blarg(…)?
Constructors rarely do anything interesting, besides accept some values which (almost always) are directly put into fields. Note that there are two common naming conventions: You can use parameter names which start with underscores (but otherwise have the meaningful name corresponding to the field):
class Blarg { int blargNum; String blargName; double blargDub = Double.MIN_VALUE: boolean blargTF; /** * Construct a Blarg, given */ Blarg( int _blargNum, String _blargName ) { this.blargNum = _blargNum; this.blargName = _blargName; this.blargTF = false; // This initial value could have been provided when // the field was declared, as with blargDub. } |
While it's technically legal to call a method (in the same class) from a constructor, you should be very careful about doing this: People writing the other methods always assume the object being worked with (this) is in a valid, fully-initialized state. But if you call it from the middle of a constructor, you're only half initialized! Even getters and setters should be avoided in this one situation.
That's all good and well, but what if this warning conflicts with our princicple of avoiding repeated code? That is, what if you have code which needs to be performed both in the constructor and in regular methods? It's an annoying conflict of interest. There is a solution, but we don't yet have the tools: static methods. Fortunately, this situation doesn't arise too often; constructors are usually extremely boring: they just initialize fields more-or-less directly from their parameters.
1 Well, this isn't the whole story, as we ignore initialization blocks, as well as not mention static (class) initializon nor superclass constructors. ↩
2Actually, they are set to a zero-ish value
(0.0 for float,
false for boolean,
).
But even if you want some field to have the value 0 initially,
you should set it yourself explicitly, so others reading your
code know what you really intended, and don't have to guess whether
you have a bug of uninitialized variables.
For this course, you'll be required to initialize every field.
↩
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 |