I am playing around with the homework assignment for my introductory programming course. We have recently learned about constructors and how they work and what they do, but I don’t understand why every object needs one. It seems to be just extra work, since you can instantiate objects and run statements outside them just as well. For example, after testing different ways to write the same code, I have found that
public class MyCartoon extends Frame {
StickFigure man = new StickFigure();
public static void main(String[] args) {
new MyCartoon();
}
}
yields exactly the same effect as this:
public class MyCartoon extends Frame {
StickFigure man;
public MyCartoon() {
man = new StickFigure();
}
public static void main(String[] args) {
new MyCartoon();
}
}
May I please get some clarification?
However, when I tried this for similar code in another class that is instantiated by the code above, it threw 100+ errors when trying to compile. The following code runs fine. The same code, except with the code within the StickFigure() constructor, does not:
import wheels.users.*;
import java.awt.Color;
public class StickFigure {
Ellipse head;
Line torso;
Line leftBackArm;
Line leftForeArm;
Line rightBackArm;
Line rightForeArm;
Line leftUpperLeg;
Line leftLowerLeg;
Line rightUpperLeg;
Line rightLowerLeg;
StickFigure() {
head = new Ellipse();
head.setColor(Color.WHITE);
head.setFrameThickness(4);
head.setFrameColor(Color.BLACK);
head.setSize(80, 80);
head.setLocation(140, 130);
torso = new Line(180, 210, 160, 340);
torso.setColor(Color.BLACK);
torso.setThickness(4);
leftBackArm = new Line(180, 210, 145, 280);
leftBackArm.setColor(Color.BLACK);
leftBackArm.setThickness(4);
leftForeArm = new Line(145, 280, 132, 340);
leftForeArm.setColor(Color.BLACK);
leftForeArm.setThickness(4);
rightBackArm = new Line(180, 210, 190, 290);
rightBackArm.setColor(Color.BLACK);
rightBackArm.setThickness(4);
rightForeArm = new Line(190, 290, 225, 350);
rightForeArm.setColor(Color.BLACK);
rightForeArm.setThickness(4);
leftUpperLeg = new Line(160, 340, 140, 420);
leftUpperLeg.setColor(Color.BLACK);
leftUpperLeg.setThickness(4);
leftLowerLeg = new Line(140, 420, 105, 490);
leftLowerLeg.setColor(Color.BLACK);
leftLowerLeg.setThickness(4);
rightUpperLeg = new Line(160, 340, 180, 420);
rightUpperLeg.setColor(Color.BLACK);
rightUpperLeg.setThickness(4);
rightLowerLeg = new Line(180, 420, 180, 500);
rightLowerLeg.setColor(Color.BLACK);
rightLowerLeg.setThickness(4);
}
}
Why would this code, as opposed to the first code block, throw errors without a provided constructor?
What you’ve shown (the use of the compiler-provided default constructor and variable initializers) is fine if:
In my experience, these are relatively rarely all true, particularly the final point. Of course if you’re happy to set properties afterwards, you can manage without – but personally I like immutable types, which almost always need constructor parameters.
So, consider how you’d write this class without constructors, but preserving all of its other features: