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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:35:50+00:00 2026-06-05T01:35:50+00:00

I realize this probably cannot be answered , but I’m looking for whether there

  • 0

I realize this probably cannot be answered, but I’m looking for whether there is some sort of guidance about whether to use private members directly or public accessors inside class methods.

For example, consider the following code (in Java, but it would look very similar in C++):

public class Matrix {

    // Private Members
    private int[][] e;
    private int numRows;
    private int numCols;

    // Accessors
    public int rows(){ return this.numRows; }
    public int cols(){ return this.numCols; }

    // Class Methods
    // ...
    public void printDimensions()
    {
        // [A] Using private members
        System.out.format("Matrix[%d*%d]\n", this.numRows, this.numCols);


        // [B] Using accessors
        System.out.format("Matrix[%d*%d]\n", this.rows(), this.cols());
    }

The printDimensions() function illustrates two ways to get the same information, [A] using private members (this.numRows, this.numCols) or [B] via accessors (this.rows(), this.cols()).

On one hand, you may prefer using the accessors since there is no way you could inadvertently change the value of the private member variables. On the other, you may prefer accessing the private members directly in hopes that it would remove the unnecessary function call.

I guess my question is, is either the de-facto standard or preferred?

  • 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-05T01:35:51+00:00Added an answer on June 5, 2026 at 1:35 am

    It’s a style call. I prefer to use accessors, because IMHO the function call overhead is small enough that in most cases it doesn’t matter, and this usage preserves the data abstraction. If i later want to change the way the data is stored, i only need to change the accessors, instead of hunting for all the places where i touched the variables.

    I don’t feel strongly about it, though, and i would break this “rule” if i thought i had a good reason to.

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

Sidebar

Related Questions

I realize this is probably a hopelessly newbie question, but what is the difference
I realize this question has probably been asked numerous times, but I have not
I realize this is probably a silly question, but... If I'm chaining a bunch
I realize this is probably a fundamental thing I should know but I am
I realize this question has been asked several times, but I wanted to frame
I realize this is more of a hardware question, but this is also very
I realize this is a rather odd request, but I was wondering if anyone
I realize this is a basic question but I have searched online, been to
I realize this sounds a little crazy, but I'm working on a project for
Before anyone says try searching, I have - I realize this is probably a

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.