Reputation: 163
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?
Upvotes: 1
Views: 186
Reputation: 1500245
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:
public class Person {
private final String name;
private final LocalDate dateOfBirth;
public Person(String name, LocalDate dateOfBirth) {
this.name = name;
this.dateOfBirth = dateOfBirth;
}
public String getName() {
return name;
}
public LocalDate getDateOfBirth() {
return dateOfBirth;
}
}
Upvotes: 10
Reputation: 213223
Its not that your first code doesn't uses a constructor. It does. Difference is that, it is using the default constructor provided by the compiler, which compiler provides in case you don't give any of yours.
Constructors are used to initialize the state of the newly created object. Either you give your constructor explicitly, or compiler adds a default one by itself.
Actually, your first code after compilation
becomes like this: -
StickFigure man;
public MyCartoon() {
super();
man = new StickFigure();
}
public static void main(String[] args) {
new MyCartoon();
}
So, you can see that, there is practically no difference at all between 1st and the 2nd code. (NOTE: - Compiler adds a super
call as first statement in every constructor you have, if you don't have your own super
or this
call)
What happens in the above code is - Compiler adds a default constructor, and then moves all the instance variable initialization that you have done at place, in the constructor.
If you have given multiple constructors (in which case, default one will not be provided by compiler), then the instance variable
initialization code is moved into every constructor by the compiler.
Upvotes: 4