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

  • Home
  • SEARCH
  • 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 6808093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:54:47+00:00 2026-05-26T19:54:47+00:00

How can we have a variable that is writable within the class but only

  • 0

How can we have a variable that is writable within the class but only “readable” outside it?

For example, instead of having to do this:

Class C {
  private int width, height;

  int GetWidth(){
    return width;
  }

  int GetHeight(){
    return height;
  }

  // etc..

I would like to do something like this:

Class C {
  public_readonly int width, height;

  // etc...

What’s the best solution?

  • 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-26T19:54:47+00:00Added an answer on May 26, 2026 at 7:54 pm

    There’s no way to do this in Java.

    Your two options (one which you mentioned) are using public getters and making the field private, or thorough documentation in the class.

    The overhead on getter methods in extremely small (if at all). If you’re doing it a large number of times, you might want to cache the fetched value instead of calling the get method.

    EDIT:

    One way to do it, although it has even more overhead than the getter, is defining a public inner class (let’s call it innerC) with a constructor that is only available to your C class, and make your fields public. That way, you can’t create innerC instances outside your class so changing your fields from outside is impossible, yet you can change them from inside. You could however read them from the outside.

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

Sidebar

Related Questions

How can I determine using PHP code that, for example, I have a variable
I need to have a variable that only one function can write (let's call
I have a variable in code that can have file path or url as
I have a variable that can either contain a list of strings or a
I have javascript string variable with var sttr=We prefer questions that can be answered
I have an unsigned int variable and it can only have the values of
I have a variable that indicates the length of time the UI can be
I have a variable that holds an angle, in degrees, which can be both
i have a variable that receive the string like this: 'BA','RJ','SC' and i need
I'm trying to parse returned information that can have a variable amount of data

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.