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 3229388
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:48:18+00:00 2026-05-17T16:48:18+00:00

Okay so I have an assignment where I have to create a class with

  • 0

Okay so I have an assignment where I have to create a class with a set of private properties, which I have done. It gets tricky because I’m fairly new to java programming (or programming in general) and am not very practiced in encapsulation. I have not used getters/setters or constructors before. I understand the getter/setter methods, but have yet to understand any of the information online for constructors, or even understand their purpose.

I have to create getters/setters and constructors for each of the properties so it would seem I should learn to use these methods. If anyone could give me an example of a class with a constructor and explain what the constructor is doing, and why I would use it it would be nice.

Thank you ahead of time.

  • 1 1 Answer
  • 3 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-05-17T16:48:18+00:00Added an answer on May 17, 2026 at 4:48 pm

    First off, welcome to the programming world 🙂

    Lets look at a simple example.

    Say you want to program a class to represent a Square. When you create a Square object, you have to give the Square certain properties, such as the length of each side. This is where constructors come into play.

    The class layout looks like this, note the length_of_each_side variable is not assigned a value.

    public class Square {
         int length_of_each_side;
    
         public Square() {
         ...
         }
    
         public Square(int length) {
         ...
         }
    }
    

    You can have a default constructor that will give you a predefined Square if you do not specify the size of the square.

    public Square() {
         length_of_each_side = 1;
    }
    

    or you can allow someone to specify the size of the square

    public Square(int length) {
         length_of_each_side = length;
    }
    

    If I want to create a pre-defined Square (length 1), it would look like this

    Square mySquare = new Square();
    

    If I want to create a Square and specify the length of 55, it would look like this

    Square mySquare = new Square(55);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I have a series of objects based on a base class which are
Okay, so I have a little assignment to do which needs some array of
Okay I have updated my code quite a bit. I am getting a new
Okay I have an assignment to find the Kth smallest element in the list
Okay, so I have an assignment to code (using Java,but I don't think that
Okay so I have a class that has 'weak typing' I.E. it can store
Okay so this is for a school assignment. I have had no problems doing
Okay I have this RewriteRule which is supposed to redirect any request for the
Okay so I've been set an assignment from university and I just can't get
This was a homework assignment problem which I know I have incorrectly answered. I

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.