简体   繁体   中英

Are constructors necessary or are they used merely for convenience?

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:

  • Your initialization never needs to throw any checked exceptions
  • You only need to assign values to variables within initialization, rather than doing anything else. (You can write initializer blocks for more complex code, but if you're going to do that you might as well make it a constructor.)
  • You don't need any parameters for your constructor

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;
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM