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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:30:39+00:00 2026-05-28T15:30:39+00:00

In classes, variables are often made private for encapsulation, and to limit the variables

  • 0

In classes, variables are often made private for encapsulation, and to limit the variables to a certain scope allow better error control and fewer bugs. This makes sense, as the fewer places a variable can be accessed the fewer places a bug can occur with that variable.

However, I always see variables made private, and then a getter and setter function used to retrieve that value (sometimes even a pointer to that variable!). For example int a is private to prevent public access, but then getA() and setA() allow direct access to them.

So don’t getter functions and setter functions defy the point of it being private? I mean private variables with accessor function are the same as public variables, only the code to access them changes. (object.variable vs object.getVariable())

Is there a reason people make variables private with accessor functions? Are there any advantages when compared to making it public?

I am talking about programming in general, but mostly in C languages (i.e. C, C++, C#, Obj-C).

  • 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-28T15:30:40+00:00Added an answer on May 28, 2026 at 3:30 pm

    The key word and tag here is “encapsulation”. You’re hiding the details of a, while still making it usable. I like the reasons already listed, there are many more. Here’s another, you’re debugging, and you find a has an incorrect value. If a is public, you’d have to check every place a is accessed. If a is private with a setter method, you know the only place a could have changed is in a call to setA() – would be a great place to put a breakpoint 😉

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

Sidebar

Related Questions

Is there any way to make certain variables in classes private (or whatever self.__var
Error: 'WithEvents' variables can only be typed as classes, interfaces or type parameters with
Often I write classes like this: Logger::Logger(bool log_time_, bool log_percentage, bool log_size):log_time(log_time_)... //made up
Most of my classes have debug variables, and this makes them often look like
How can we access and reference classes and variables in different pages in ASP.NET?
Is it good practice to let abstract classes define instance variables? public abstract class
I would like to make all my public variables variables/methods/classes internal when compiling my
I am designing some immutable classes but I have to have some variables like
when we write all our application methods and variables in a classes is nothing
If you disassemble C# you often see temporary variables inserted by the compiler with

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.