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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:32:27+00:00 2026-05-15T18:32:27+00:00

Every time I write trivial getters (get functions that just return the value of

  • 0

Every time I write trivial getters (get functions that just return the value of the member) I wonder why don’t oop languages simply have a ‘read only’ access modifier that would allow reading the value of the members of the object but does not allow you to set them just like const things in c++.

The private,protected,public access modifiers gives you either full (read/write) access or no access.

Writing a getter and calling it every time is slow, because function calling is slower than just accessing a member. A good optimizer can optimize these getter calls out but this is ‘magic’. And I don’t think it is good idea learning how an optimizer of a certain compiler works and write code to exploit it.

So why do we need to write accessors, read only interfaces everywhere in practice when just a new access modifier would do the trick?

ps1: please don’t tell things like ‘It would break the encapsulation’. A public foo.getX() and a public but read only foo.x would do the same thing.

EDIT: I didn’t composed my post clear. Sorry. I mean you can read the member’s value outside but you can’t set it. You can only set its value inside the class scope.

  • 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-15T18:32:28+00:00Added an answer on May 15, 2026 at 6:32 pm

    You’re incorrectly generalizing from one or some OOP language(s) you know to OOP languages in general. Some examples of languages that implement read-only attributes:

    • C# (thanks, Darin and tonio)
    • Delphi (= Object Pascal)
    • Ruby
    • Scala
    • Objective-C (thanks, Rano)
    • … more?

    Personally, I’m annoyed that Java doesn’t have this (yet?). Having seen the feature in other languages makes boilerplate writing in Java seem tiresome.

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

Sidebar

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.