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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:56:49+00:00 2026-05-31T08:56:49+00:00

From what I understand, getters and setters are recommended for use for ALL variables

  • 0

From what I understand, getters and setters are recommended for use for ALL variables and situations.

However I have a situation here where it seems they are just making the code long and awkward so I want to know can I leave them out in this situation or is that bad practise…

I have the following class –

public class Conversation {

    class Message
    {
        private int id;
        private int senderId;
        private Timestamp timeSent;
        private String text;
    }

    private int conversationId;
    private int userIdA;
    private int userIdB;
    private ArrayList<Message> messages = new ArrayList<Message>();
...
... (Getters and setters for members of Conversation)
...
}

So I am using getters and setters for the Conversation member variables. However I don’t want to use them for inner class, Message, variables. It is just adding about 30 lines to the code of filler and makes it look much more awkward. So is it bad practise to leave them out here? I will also never be editing the Message variables, they will be set with a call to the constructor Message(…, …, …, …) and after that they will just be read. So I definitely reckon it’s fine to leave out the getters, but what about the setters?

  • 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-31T08:56:50+00:00Added an answer on May 31, 2026 at 8:56 am

    If Message were private, I would say no problem at all with using the fields directly; it’s just an internal-details struct, and there are no encapsulation issues (since the outer class can see all of the inner class’ private variables, anyway).

    But, Message is package-private: anyone in that package can also see its fields. This is a gray area, because it’s not quite the published API, but it’s still reasonable, depending on how big the package is, that you’ll want encapsulation down the line. For instance, if it’s a big package and some other class starts depending on Message (and if none do, you may as well make it private), then you risk encapsulation problems if you want to change Message‘s internals down the line. So, it’s a judgement call.

    If Message were public, I would definitely say to provide getters.

    Btw, if you’re using Message just as a POD here, you should make it static — good habit to get into. Generally speaking, static is better than not.

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

Sidebar

Related Questions

I found that Maven implies specific directory layout. But I don't understand from here:
From what I understand, the trade-off here is a matter of added complexity. Maybe?
From what I understand, in standard C++ whenever you use the new operator you
Possible Duplicate: Why use getters and setters? In C#(ASP.NET) we use getter and setter
I have the following classes in my JPA model (getters, setters, and irrelevant fields
From what I understand I should always use self.property_name everywhere except in getter and
I don't really have a solid understanding of Setters and Getters for objective-c. Can
From what I understand of REST principles, URLs should represent a single resource, like
From what I understand, the best way to deal with dates in the Zend
From what I understand livequery is for maintaining your events after DOM changes. Does

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.