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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:07:16+00:00 2026-05-16T10:07:16+00:00

I have the following class which I’m fighting over how to implement. The question

  • 0

I have the following class which I’m fighting over how to implement. The question is whether or not it makes sense to have a private collection item, in this case an arraylist, a a member. I am well aware it is considered best practice to have getters and setters for any class members, but in this case having a getter and setter would require re-implementing (or rather duplicating) large amounts of the ArrayList functionality.

Example class:

public class Email
{
    private ArrayList<String> To;
    private ArrayList<String> Cc;
    private ArrayList<String> Bcc;

    ...
}

I assume the answer is I should indeed implement getters and setters for these arrays? Is there some approach I haven’t thought of regarding handling this type of situation? The easy solution to managing these lists is to set the private modifiers to public and check the array data is valid on calling methods, but is this right? Can anyone point me in the direction of other SO questions, design patterns etc I should consider?

  • 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-05-16T10:07:17+00:00Added an answer on May 16, 2026 at 10:07 am

    Actually in that case I think it would be better to not provide a direct getter/setter combination.

    I would rather approach it the following way:

    1. Change the type to List<String> (best practice to use Interfaces on fields.)
    2. Initialize an empty ArrayLists in the constructor.
    3. Provide delegates for add, remove, hasTo/Cc/Bcc.

    In that way you could decorate each collection with additional functionality, e.g. making sure the String contains a valid recipient information.

    public void addTo(String recipient){
      // validate recipient
      this.to.add(recipient);
    }
    
    public String removeTo(String recipient){
      return this.to.remove(recipient);
    }
    
    public boolean hasTo(){
      return this.to.size() > 0;
    }
    

    EDIT

    I forgot to mention that a getter makes sense since you need that when processing the Email. But look at the other answer on considering to returning an unmodifiable List.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following asynctask class which is not inside the activity. In the
I have a class which must implement the following property public ICollection<IType> Items {
I have the following abstract class which CAN NOT be changed. public abstract class
Let's say I have the following class which I am not allowed to change:
I have the following class which handles my user logged in / logged out
I have the following javascript class which I am trying to pass to an
In Java I have created the following class which extends ImageView to create a
I have the following tricky problem: I have implemented a (rather complicated) class which
Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in
I have a class which loads an xml file using the following: Path.Combine( AppDomain.CurrentDomain.BaseDirectory,

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.