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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:24:39+00:00 2026-05-31T03:24:39+00:00

I have been reading about getters and setters and I have a question. Are

  • 0

I have been reading about getters and setters and I have a question. Are getters and setters supposed to be used when accessing the variables in the class that declared the variables? It seems like there is no need for getters and setters here because it doesnt matter that the variable is private, the class that declared it will always have access to it….?

Or are getters and setters only supposed to be used when external classes want to access these variables?

  • 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-31T03:24:40+00:00Added an answer on May 31, 2026 at 3:24 am

    While the language doesn’t require you to do so, it is good practice to use getters and setters when accessing private fields that are exposed through getters and setters. In some cases I would even go so far as to say that it makes sense to use them even for internal properties that are not directly exposed (yet).

    The reason it is good practice to do so is because it isolates the code that reads and modifies the private fields to a single set of methods. That way you can later provide extra validation, or even change the way the property is stored internally without having to change too many places.

    An example of a change might be a class that exposes, through getter (accessor) / setter (mutator) methods, a certain property that is initially stored as a private field in that class. Later you realise that you need to use a different storage repository for that property – maybe read it from a file, or from a database etc. At that point if you’ve only used the methods to access and modify the property you can simply change the code of the accessor and mutator methods to implement the change.

    Another example would be instances when the class is extended. It provides better encapsulation.

    And even for testing it makes sense to abstract access to the private “storage repository” for a logical property.

    Note: I’m referring to the concept of the private member you’re exposing as a property of the class even though Java doesn’t necessarily refer to them as such.

    Finally, I can’t stress enough that my recommendation to use methods instead of directly accessing the private member is just that: a recommendation. It’s considered by many to be good practice and as such I recommend you follow, but by all means feel free to not follow if you have a good reason not too!

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

Sidebar

Related Questions

i have been reading about network flooding algorithm that it is used to discover
I have been reading about MVP, MVVM and have used asp.net MVC in couple
I have been reading about Quicksort and found that sometimes it' s referred to
i have been reading about a function that can overwrite its return address. void
I have been reading about JPA and EJB3 and would like to confirm that
I have been reading about css and js compression and minimizing. In particular that
I have been reading about threadpools. A number of sites say that the default
I have been reading about the function of __init__.py file. It is said that
I have been reading about the differences between Table Variables and Temp Tables and
I have been reading about google Cloud Messaging and as a developer my question

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.