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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:57:49+00:00 2026-06-08T06:57:49+00:00

Possible Duplicate: Why use getters and setters? I know this is very trivial. But

  • 0

Possible Duplicate:
Why use getters and setters?

I know this is very trivial. But why do we define private and then we use getter and setters. Is this more like for preventing the programmers from making mistakes on using private variables or methods? Or is it for security reasons. If for security reasons then what is the point of having getters or setters? I know that we can have restrictions inside getter and setter but those if clauses are mostly for preventing the mistakes not the privacy restrictions. E.g. we don’t usually say for these members limit the access to this method with an if clause.

Thanks.

  • 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-08T06:57:51+00:00Added an answer on June 8, 2026 at 6:57 am

    The practice of automatically, unthinkingly creating getX() and setX() methods for every variable is bad. It’s mostly pointless, as you have observed.

    But it’s the unthinking part that’s bad. Methods are infinitely better than exposed variables because, again, as you observed, you can control how the underlying data is accessed. If you think about what you’re doing, and apply mutators and accessors as appropriate, they’re a wonderful tool:

    • You can restrict clients to only reading, but not writing to a variable.
    • You can restrict the values of an integer to only positive values (for example).
    • You can ensure that one variable is always equal to one-half the value of another (for example).
    • You can ensure that, any time the value of a variable is changed, an event is sent to notify other clients.
    • You can change the data type of an underlying variable, perform conversions in the public methods, and not break any clients.
    • You can convert an object to a remote client for a server-side version of the same object, and again, the client code won’t change.
    • You can ensure strict memory ordering across threads by using synchronized accessors.

    These are only some of the things you can do with mutators and accessors. By using them, you make your software easy to change and easy to maintain.

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

Sidebar

Related Questions

Possible Duplicate: Why use getters and setters? Yes, It's a very simple thing but
Possible Duplicate: Why use getters and setters? In C#(ASP.NET) we use getter and setter
Possible Duplicate: Why use getters and setters? I see this a fair bit in
Possible Duplicate: Why use getters and setters? Is there any advantage to making methods
Possible Duplicate: Why use getters and setters? I have read books on Java, saying
Possible Duplicate: Why use pointers? I know what the C++ & does. but what
Possible Duplicate: Is it really that wrong not using setters and getters? Why use
Possible Duplicate: Why use getters and setters? I'm reading the Java for Dummies 2nd
Possible Duplicate: When to use C++ private inheritance over composition? Please help me with
Possible Duplicate: Convention question: When do you use a Getter/Setter function rather than using

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.