Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8773175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:13:39+00:00 2026-06-13T18:13:39+00:00

I am playing around with the homework assignment for my introductory programming course. We

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:13:41+00:00Added an answer on June 13, 2026 at 6:13 pm

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

playing around with a new samsung tablet i noticed they have mini apps on
Just playing around with interfaces and I have a question about something which I
I recently just started playing around in PHP and got myself a small project/homework.
Im playing around with TryParse() But lets say the parsing fails, then returns false,
Playing around with Google Maps these days, with some directions. I have a map
Lately I have been playing around with intents and bundles. I thought I had
Playing around with SQL Server 2008 and I have a table People with columns:
Recently playing around with the open source iphone app code, and found it uses
In playing around with Xcode recently, there is a framework named JavaVM.framework . What
Playing around in order to learn XSLT, I have the following XML file and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.